1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/device.h>
5 #include <linux/errno.h>
7 #include <linux/fsi-sbefifo.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/miscdevice.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/fsi-occ.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <asm/unaligned.h>
25 #define OCC_SRAM_BYTES 4096
26 #define OCC_CMD_DATA_BYTES 4090
27 #define OCC_RESP_DATA_BYTES 4089
29 #define OCC_P9_SRAM_CMD_ADDR 0xFFFBE000
30 #define OCC_P9_SRAM_RSP_ADDR 0xFFFBF000
32 #define OCC_P10_SRAM_CMD_ADDR 0xFFFFD000
33 #define OCC_P10_SRAM_RSP_ADDR 0xFFFFE000
35 #define OCC_P10_SRAM_MODE 0x58 /* Normal mode, OCB channel 2 */
37 #define OCC_TIMEOUT_MS 1000
38 #define OCC_CMD_IN_PRG_WAIT_MS 50
40 enum versions { occ_p9, occ_p10 };
44 struct device *sbefifo;
49 enum versions version;
50 struct miscdevice mdev;
51 struct mutex occ_lock;
54 #define to_occ(x) container_of((x), struct occ, mdev)
61 u8 data[OCC_RESP_DATA_BYTES + 2]; /* two bytes checksum */
72 #define to_client(x) container_of((x), struct occ_client, xfr)
74 static DEFINE_IDA(occ_ida);
76 static int occ_open(struct inode *inode, struct file *file)
78 struct occ_client *client = kzalloc(sizeof(*client), GFP_KERNEL);
79 struct miscdevice *mdev = file->private_data;
80 struct occ *occ = to_occ(mdev);
85 client->buffer = (u8 *)__get_free_page(GFP_KERNEL);
86 if (!client->buffer) {
92 mutex_init(&client->lock);
93 file->private_data = client;
95 /* We allocate a 1-page buffer, make sure it all fits */
96 BUILD_BUG_ON((OCC_CMD_DATA_BYTES + 3) > PAGE_SIZE);
97 BUILD_BUG_ON((OCC_RESP_DATA_BYTES + 7) > PAGE_SIZE);
102 static ssize_t occ_read(struct file *file, char __user *buf, size_t len,
105 struct occ_client *client = file->private_data;
111 if (len > OCC_SRAM_BYTES)
114 mutex_lock(&client->lock);
116 /* This should not be possible ... */
117 if (WARN_ON_ONCE(client->read_offset > client->data_size)) {
122 /* Grab how much data we have to read */
123 rc = min(len, client->data_size - client->read_offset);
124 if (copy_to_user(buf, client->buffer + client->read_offset, rc))
127 client->read_offset += rc;
130 mutex_unlock(&client->lock);
135 static ssize_t occ_write(struct file *file, const char __user *buf,
136 size_t len, loff_t *offset)
138 struct occ_client *client = file->private_data;
139 size_t rlen, data_length;
146 if (len > (OCC_CMD_DATA_BYTES + 3) || len < 3)
149 mutex_lock(&client->lock);
151 /* Construct the command */
152 cmd = client->buffer;
155 * Copy the user command (assume user data follows the occ command
157 * byte 0: command type
158 * bytes 1-2: data length (msb first)
161 if (copy_from_user(&cmd[1], buf, len)) {
166 /* Extract data length */
167 data_length = (cmd[2] << 8) + cmd[3];
168 if (data_length > OCC_CMD_DATA_BYTES) {
173 /* Submit command; 4 bytes before the data and 2 bytes after */
175 rc = fsi_occ_submit(client->occ->dev, cmd, data_length + 6, cmd,
180 /* Set read tracking data */
181 client->data_size = rlen;
182 client->read_offset = 0;
188 mutex_unlock(&client->lock);
193 static int occ_release(struct inode *inode, struct file *file)
195 struct occ_client *client = file->private_data;
197 free_page((unsigned long)client->buffer);
203 static const struct file_operations occ_fops = {
204 .owner = THIS_MODULE,
208 .release = occ_release,
211 static int occ_verify_checksum(struct occ *occ, struct occ_response *resp,
214 /* Fetch the two bytes after the data for the checksum. */
215 u16 checksum_resp = get_unaligned_be16(&resp->data[data_length]);
219 checksum = resp->seq_no;
220 checksum += resp->cmd_type;
221 checksum += resp->return_status;
222 checksum += (data_length >> 8) + (data_length & 0xFF);
224 for (i = 0; i < data_length; ++i)
225 checksum += resp->data[i];
227 if (checksum != checksum_resp) {
228 dev_err(occ->dev, "Bad checksum: %04x!=%04x\n", checksum,
236 static int occ_getsram(struct occ *occ, u32 offset, void *data, ssize_t len)
238 u32 data_len = ((len + 7) / 8) * 8; /* must be multiples of 8 B */
239 size_t cmd_len, resp_data_len;
240 size_t resp_len = OCC_MAX_RESP_WORDS;
241 __be32 *resp = occ->buffer;
246 * Magic sequence to do SBE getsram command. SBE will fetch data from
247 * specified SRAM address.
249 switch (occ->version) {
253 cmd[2] = cpu_to_be32(1); /* Normal mode */
254 cmd[3] = cpu_to_be32(OCC_P9_SRAM_RSP_ADDR + offset);
259 cmd[2] = cpu_to_be32(OCC_P10_SRAM_MODE);
261 cmd[4] = cpu_to_be32(OCC_P10_SRAM_RSP_ADDR + offset);
265 cmd[0] = cpu_to_be32(cmd_len);
266 cmd[1] = cpu_to_be32(SBEFIFO_CMD_GET_OCC_SRAM);
267 cmd[4 + idx] = cpu_to_be32(data_len);
269 rc = sbefifo_submit(occ->sbefifo, cmd, cmd_len, resp, &resp_len);
273 rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_GET_OCC_SRAM,
274 resp, resp_len, &resp_len);
276 dev_err(occ->dev, "SRAM read returned failure status: %08x\n",
283 resp_data_len = be32_to_cpu(resp[resp_len - 1]);
284 if (resp_data_len != data_len) {
285 dev_err(occ->dev, "SRAM read expected %d bytes got %zd\n",
286 data_len, resp_data_len);
289 memcpy(data, resp, len);
295 static int occ_putsram(struct occ *occ, const void *data, ssize_t len,
296 u8 seq_no, u16 checksum)
298 u32 data_len = ((len + 7) / 8) * 8; /* must be multiples of 8 B */
299 size_t cmd_len, resp_data_len;
300 size_t resp_len = OCC_MAX_RESP_WORDS;
301 __be32 *buf = occ->buffer;
305 cmd_len = (occ->version == occ_p10) ? 6 : 5;
306 cmd_len += data_len >> 2;
309 * Magic sequence to do SBE putsram command. SBE will transfer
310 * data to specified SRAM address.
312 buf[0] = cpu_to_be32(cmd_len);
313 buf[1] = cpu_to_be32(SBEFIFO_CMD_PUT_OCC_SRAM);
315 switch (occ->version) {
318 buf[2] = cpu_to_be32(1); /* Normal mode */
319 buf[3] = cpu_to_be32(OCC_P9_SRAM_CMD_ADDR);
323 buf[2] = cpu_to_be32(OCC_P10_SRAM_MODE);
325 buf[4] = cpu_to_be32(OCC_P10_SRAM_CMD_ADDR);
329 buf[4 + idx] = cpu_to_be32(data_len);
330 memcpy(&buf[5 + idx], data, len);
332 byte_buf = (u8 *)&buf[5 + idx];
334 * Overwrite the first byte with our sequence number and the last two
335 * bytes with the checksum.
337 byte_buf[0] = seq_no;
338 byte_buf[len - 2] = checksum >> 8;
339 byte_buf[len - 1] = checksum & 0xff;
341 rc = sbefifo_submit(occ->sbefifo, buf, cmd_len, buf, &resp_len);
345 rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_PUT_OCC_SRAM,
346 buf, resp_len, &resp_len);
348 dev_err(occ->dev, "SRAM write returned failure status: %08x\n",
356 dev_err(occ->dev, "SRAM write response length invalid: %zd\n",
360 resp_data_len = be32_to_cpu(buf[0]);
361 if (resp_data_len != data_len) {
363 "SRAM write expected %d bytes got %zd\n",
364 data_len, resp_data_len);
372 static int occ_trigger_attn(struct occ *occ)
374 __be32 *buf = occ->buffer;
375 size_t cmd_len, resp_data_len;
376 size_t resp_len = OCC_MAX_RESP_WORDS;
379 switch (occ->version) {
383 buf[2] = cpu_to_be32(3); /* Circular mode */
389 buf[2] = cpu_to_be32(0xd0); /* Circular mode, OCB Channel 1 */
395 buf[0] = cpu_to_be32(cmd_len); /* Chip-op length in words */
396 buf[1] = cpu_to_be32(SBEFIFO_CMD_PUT_OCC_SRAM);
397 buf[4 + idx] = cpu_to_be32(8); /* Data length in bytes */
398 buf[5 + idx] = cpu_to_be32(0x20010000); /* Trigger OCC attention */
401 rc = sbefifo_submit(occ->sbefifo, buf, cmd_len, buf, &resp_len);
405 rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_PUT_OCC_SRAM,
406 buf, resp_len, &resp_len);
408 dev_err(occ->dev, "SRAM attn returned failure status: %08x\n",
416 dev_err(occ->dev, "SRAM attn response length invalid: %zd\n",
420 resp_data_len = be32_to_cpu(buf[0]);
421 if (resp_data_len != 8) {
423 "SRAM attn expected 8 bytes got %zd\n",
432 int fsi_occ_submit(struct device *dev, const void *request, size_t req_len,
433 void *response, size_t *resp_len)
435 const unsigned long timeout = msecs_to_jiffies(OCC_TIMEOUT_MS);
436 const unsigned long wait_time =
437 msecs_to_jiffies(OCC_CMD_IN_PRG_WAIT_MS);
438 struct occ *occ = dev_get_drvdata(dev);
439 struct occ_response *resp = response;
442 u16 resp_data_length;
443 const u8 *byte_request = (const u8 *)request;
452 dev_dbg(dev, "Bad resplen %zd\n", *resp_len);
456 /* Checksum the request, ignoring first byte (sequence number). */
457 for (i = 1; i < req_len - 2; ++i)
458 checksum += byte_request[i];
460 mutex_lock(&occ->occ_lock);
463 * Get a sequence number and update the counter. Avoid a sequence
464 * number of 0 which would pass the response check below even if the
465 * OCC response is uninitialized. Any sequence number the user is
466 * trying to send is overwritten since this function is the only common
467 * interface to the OCC and therefore the only place we can guarantee
468 * unique sequence numbers.
470 seq_no = occ->sequence_number++;
471 if (!occ->sequence_number)
472 occ->sequence_number = 1;
475 rc = occ_putsram(occ, request, req_len, seq_no, checksum);
479 rc = occ_trigger_attn(occ);
483 /* Read occ response header */
486 rc = occ_getsram(occ, 0, resp, 8);
490 if (resp->return_status == OCC_RESP_CMD_IN_PRG ||
491 resp->return_status == OCC_RESP_CRIT_INIT ||
492 resp->seq_no != seq_no) {
495 if (time_after(jiffies, start + timeout)) {
496 dev_err(occ->dev, "resp timeout status=%02x "
497 "resp seq_no=%d our seq_no=%d\n",
498 resp->return_status, resp->seq_no,
503 set_current_state(TASK_UNINTERRUPTIBLE);
504 schedule_timeout(wait_time);
508 /* Extract size of response data */
509 resp_data_length = get_unaligned_be16(&resp->data_length);
511 /* Message size is data length + 5 bytes header + 2 bytes checksum */
512 if ((resp_data_length + 7) > *resp_len) {
517 dev_dbg(dev, "resp_status=%02x resp_data_len=%d\n",
518 resp->return_status, resp_data_length);
521 if (resp_data_length > 1) {
522 /* already got 3 bytes resp, also need 2 bytes checksum */
523 rc = occ_getsram(occ, 8, &resp->data[3], resp_data_length - 1);
528 *resp_len = resp_data_length + 7;
529 rc = occ_verify_checksum(occ, resp, resp_data_length);
532 mutex_unlock(&occ->occ_lock);
536 EXPORT_SYMBOL_GPL(fsi_occ_submit);
538 static int occ_unregister_child(struct device *dev, void *data)
540 struct platform_device *hwmon_dev = to_platform_device(dev);
542 platform_device_unregister(hwmon_dev);
547 static int occ_probe(struct platform_device *pdev)
552 struct platform_device *hwmon_dev;
553 struct device *dev = &pdev->dev;
554 struct platform_device_info hwmon_dev_info = {
559 occ = devm_kzalloc(dev, sizeof(*occ), GFP_KERNEL);
563 /* SBE words are always four bytes */
564 occ->buffer = kvmalloc(OCC_MAX_RESP_WORDS * 4, GFP_KERNEL);
568 occ->version = (uintptr_t)of_device_get_match_data(dev);
570 occ->sbefifo = dev->parent;
571 occ->sequence_number = 1;
572 mutex_init(&occ->occ_lock);
575 rc = of_property_read_u32(dev->of_node, "reg", ®);
577 /* make sure we don't have a duplicate from dts */
578 occ->idx = ida_simple_get(&occ_ida, reg, reg + 1,
581 occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX,
584 occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX,
588 occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX, GFP_KERNEL);
591 platform_set_drvdata(pdev, occ);
593 snprintf(occ->name, sizeof(occ->name), "occ%d", occ->idx);
594 occ->mdev.fops = &occ_fops;
595 occ->mdev.minor = MISC_DYNAMIC_MINOR;
596 occ->mdev.name = occ->name;
597 occ->mdev.parent = dev;
599 rc = misc_register(&occ->mdev);
601 dev_err(dev, "failed to register miscdevice: %d\n", rc);
602 ida_simple_remove(&occ_ida, occ->idx);
607 hwmon_dev_info.id = occ->idx;
608 hwmon_dev = platform_device_register_full(&hwmon_dev_info);
609 if (IS_ERR(hwmon_dev))
610 dev_warn(dev, "failed to create hwmon device\n");
615 static int occ_remove(struct platform_device *pdev)
617 struct occ *occ = platform_get_drvdata(pdev);
621 misc_deregister(&occ->mdev);
623 device_for_each_child(&pdev->dev, NULL, occ_unregister_child);
625 ida_simple_remove(&occ_ida, occ->idx);
630 static const struct of_device_id occ_match[] = {
632 .compatible = "ibm,p9-occ",
633 .data = (void *)occ_p9
636 .compatible = "ibm,p10-occ",
637 .data = (void *)occ_p10
641 MODULE_DEVICE_TABLE(of, occ_match);
643 static struct platform_driver occ_driver = {
646 .of_match_table = occ_match,
649 .remove = occ_remove,
652 static int occ_init(void)
654 return platform_driver_register(&occ_driver);
657 static void occ_exit(void)
659 platform_driver_unregister(&occ_driver);
661 ida_destroy(&occ_ida);
664 module_init(occ_init);
665 module_exit(occ_exit);
667 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
668 MODULE_DESCRIPTION("BMC P9 OCC driver");
669 MODULE_LICENSE("GPL");