18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * OPAL PNOR flash MTD abstraction 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright IBM 2015 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/errno.h> 118c2ecf20Sopenharmony_ci#include <linux/of.h> 128c2ecf20Sopenharmony_ci#include <linux/of_address.h> 138c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 148c2ecf20Sopenharmony_ci#include <linux/string.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h> 178c2ecf20Sopenharmony_ci#include <linux/mtd/partitions.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <linux/debugfs.h> 208c2ecf20Sopenharmony_ci#include <linux/seq_file.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <asm/opal.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* 268c2ecf20Sopenharmony_ci * This driver creates the a Linux MTD abstraction for platform PNOR flash 278c2ecf20Sopenharmony_ci * backed by OPAL calls 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistruct powernv_flash { 318c2ecf20Sopenharmony_ci struct mtd_info mtd; 328c2ecf20Sopenharmony_ci u32 id; 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cienum flash_op { 368c2ecf20Sopenharmony_ci FLASH_OP_READ, 378c2ecf20Sopenharmony_ci FLASH_OP_WRITE, 388c2ecf20Sopenharmony_ci FLASH_OP_ERASE, 398c2ecf20Sopenharmony_ci}; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* 428c2ecf20Sopenharmony_ci * Don't return -ERESTARTSYS if we can't get a token, the MTD core 438c2ecf20Sopenharmony_ci * might have split up the call from userspace and called into the 448c2ecf20Sopenharmony_ci * driver more than once, we'll already have done some amount of work. 458c2ecf20Sopenharmony_ci */ 468c2ecf20Sopenharmony_cistatic int powernv_flash_async_op(struct mtd_info *mtd, enum flash_op op, 478c2ecf20Sopenharmony_ci loff_t offset, size_t len, size_t *retlen, u_char *buf) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci struct powernv_flash *info = (struct powernv_flash *)mtd->priv; 508c2ecf20Sopenharmony_ci struct device *dev = &mtd->dev; 518c2ecf20Sopenharmony_ci int token; 528c2ecf20Sopenharmony_ci struct opal_msg msg; 538c2ecf20Sopenharmony_ci int rc; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci dev_dbg(dev, "%s(op=%d, offset=0x%llx, len=%zu)\n", 568c2ecf20Sopenharmony_ci __func__, op, offset, len); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci token = opal_async_get_token_interruptible(); 598c2ecf20Sopenharmony_ci if (token < 0) { 608c2ecf20Sopenharmony_ci if (token != -ERESTARTSYS) 618c2ecf20Sopenharmony_ci dev_err(dev, "Failed to get an async token\n"); 628c2ecf20Sopenharmony_ci else 638c2ecf20Sopenharmony_ci token = -EINTR; 648c2ecf20Sopenharmony_ci return token; 658c2ecf20Sopenharmony_ci } 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci switch (op) { 688c2ecf20Sopenharmony_ci case FLASH_OP_READ: 698c2ecf20Sopenharmony_ci rc = opal_flash_read(info->id, offset, __pa(buf), len, token); 708c2ecf20Sopenharmony_ci break; 718c2ecf20Sopenharmony_ci case FLASH_OP_WRITE: 728c2ecf20Sopenharmony_ci rc = opal_flash_write(info->id, offset, __pa(buf), len, token); 738c2ecf20Sopenharmony_ci break; 748c2ecf20Sopenharmony_ci case FLASH_OP_ERASE: 758c2ecf20Sopenharmony_ci rc = opal_flash_erase(info->id, offset, len, token); 768c2ecf20Sopenharmony_ci break; 778c2ecf20Sopenharmony_ci default: 788c2ecf20Sopenharmony_ci WARN_ON_ONCE(1); 798c2ecf20Sopenharmony_ci opal_async_release_token(token); 808c2ecf20Sopenharmony_ci return -EIO; 818c2ecf20Sopenharmony_ci } 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci if (rc == OPAL_ASYNC_COMPLETION) { 848c2ecf20Sopenharmony_ci rc = opal_async_wait_response_interruptible(token, &msg); 858c2ecf20Sopenharmony_ci if (rc) { 868c2ecf20Sopenharmony_ci /* 878c2ecf20Sopenharmony_ci * If we return the mtd core will free the 888c2ecf20Sopenharmony_ci * buffer we've just passed to OPAL but OPAL 898c2ecf20Sopenharmony_ci * will continue to read or write from that 908c2ecf20Sopenharmony_ci * memory. 918c2ecf20Sopenharmony_ci * It may be tempting to ultimately return 0 928c2ecf20Sopenharmony_ci * if we're doing a read or a write since we 938c2ecf20Sopenharmony_ci * are going to end up waiting until OPAL is 948c2ecf20Sopenharmony_ci * done. However, because the MTD core sends 958c2ecf20Sopenharmony_ci * us the userspace request in chunks, we need 968c2ecf20Sopenharmony_ci * it to know we've been interrupted. 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_ci rc = -EINTR; 998c2ecf20Sopenharmony_ci if (opal_async_wait_response(token, &msg)) 1008c2ecf20Sopenharmony_ci dev_err(dev, "opal_async_wait_response() failed\n"); 1018c2ecf20Sopenharmony_ci goto out; 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci rc = opal_get_async_rc(msg); 1048c2ecf20Sopenharmony_ci } 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci /* 1078c2ecf20Sopenharmony_ci * OPAL does mutual exclusion on the flash, it will return 1088c2ecf20Sopenharmony_ci * OPAL_BUSY. 1098c2ecf20Sopenharmony_ci * During firmware updates by the service processor OPAL may 1108c2ecf20Sopenharmony_ci * be (temporarily) prevented from accessing the flash, in 1118c2ecf20Sopenharmony_ci * this case OPAL will also return OPAL_BUSY. 1128c2ecf20Sopenharmony_ci * Both cases aren't errors exactly but the flash could have 1138c2ecf20Sopenharmony_ci * changed, userspace should be informed. 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_ci if (rc != OPAL_SUCCESS && rc != OPAL_BUSY) 1168c2ecf20Sopenharmony_ci dev_err(dev, "opal_flash_async_op(op=%d) failed (rc %d)\n", 1178c2ecf20Sopenharmony_ci op, rc); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci if (rc == OPAL_SUCCESS && retlen) 1208c2ecf20Sopenharmony_ci *retlen = len; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci rc = opal_error_code(rc); 1238c2ecf20Sopenharmony_ciout: 1248c2ecf20Sopenharmony_ci opal_async_release_token(token); 1258c2ecf20Sopenharmony_ci return rc; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci/** 1298c2ecf20Sopenharmony_ci * @mtd: the device 1308c2ecf20Sopenharmony_ci * @from: the offset to read from 1318c2ecf20Sopenharmony_ci * @len: the number of bytes to read 1328c2ecf20Sopenharmony_ci * @retlen: the number of bytes actually read 1338c2ecf20Sopenharmony_ci * @buf: the filled in buffer 1348c2ecf20Sopenharmony_ci * 1358c2ecf20Sopenharmony_ci * Returns 0 if read successful, or -ERRNO if an error occurred 1368c2ecf20Sopenharmony_ci */ 1378c2ecf20Sopenharmony_cistatic int powernv_flash_read(struct mtd_info *mtd, loff_t from, size_t len, 1388c2ecf20Sopenharmony_ci size_t *retlen, u_char *buf) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci return powernv_flash_async_op(mtd, FLASH_OP_READ, from, 1418c2ecf20Sopenharmony_ci len, retlen, buf); 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci/** 1458c2ecf20Sopenharmony_ci * @mtd: the device 1468c2ecf20Sopenharmony_ci * @to: the offset to write to 1478c2ecf20Sopenharmony_ci * @len: the number of bytes to write 1488c2ecf20Sopenharmony_ci * @retlen: the number of bytes actually written 1498c2ecf20Sopenharmony_ci * @buf: the buffer to get bytes from 1508c2ecf20Sopenharmony_ci * 1518c2ecf20Sopenharmony_ci * Returns 0 if write successful, -ERRNO if error occurred 1528c2ecf20Sopenharmony_ci */ 1538c2ecf20Sopenharmony_cistatic int powernv_flash_write(struct mtd_info *mtd, loff_t to, size_t len, 1548c2ecf20Sopenharmony_ci size_t *retlen, const u_char *buf) 1558c2ecf20Sopenharmony_ci{ 1568c2ecf20Sopenharmony_ci return powernv_flash_async_op(mtd, FLASH_OP_WRITE, to, 1578c2ecf20Sopenharmony_ci len, retlen, (u_char *)buf); 1588c2ecf20Sopenharmony_ci} 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci/** 1618c2ecf20Sopenharmony_ci * @mtd: the device 1628c2ecf20Sopenharmony_ci * @erase: the erase info 1638c2ecf20Sopenharmony_ci * Returns 0 if erase successful or -ERRNO if an error occurred 1648c2ecf20Sopenharmony_ci */ 1658c2ecf20Sopenharmony_cistatic int powernv_flash_erase(struct mtd_info *mtd, struct erase_info *erase) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci int rc; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci rc = powernv_flash_async_op(mtd, FLASH_OP_ERASE, erase->addr, 1708c2ecf20Sopenharmony_ci erase->len, NULL, NULL); 1718c2ecf20Sopenharmony_ci if (rc) 1728c2ecf20Sopenharmony_ci erase->fail_addr = erase->addr; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci return rc; 1758c2ecf20Sopenharmony_ci} 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci/** 1788c2ecf20Sopenharmony_ci * powernv_flash_set_driver_info - Fill the mtd_info structure and docg3 1798c2ecf20Sopenharmony_ci * structure @pdev: The platform device 1808c2ecf20Sopenharmony_ci * @mtd: The structure to fill 1818c2ecf20Sopenharmony_ci */ 1828c2ecf20Sopenharmony_cistatic int powernv_flash_set_driver_info(struct device *dev, 1838c2ecf20Sopenharmony_ci struct mtd_info *mtd) 1848c2ecf20Sopenharmony_ci{ 1858c2ecf20Sopenharmony_ci u64 size; 1868c2ecf20Sopenharmony_ci u32 erase_size; 1878c2ecf20Sopenharmony_ci int rc; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci rc = of_property_read_u32(dev->of_node, "ibm,flash-block-size", 1908c2ecf20Sopenharmony_ci &erase_size); 1918c2ecf20Sopenharmony_ci if (rc) { 1928c2ecf20Sopenharmony_ci dev_err(dev, "couldn't get resource block size information\n"); 1938c2ecf20Sopenharmony_ci return rc; 1948c2ecf20Sopenharmony_ci } 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci rc = of_property_read_u64(dev->of_node, "reg", &size); 1978c2ecf20Sopenharmony_ci if (rc) { 1988c2ecf20Sopenharmony_ci dev_err(dev, "couldn't get resource size information\n"); 1998c2ecf20Sopenharmony_ci return rc; 2008c2ecf20Sopenharmony_ci } 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci /* 2038c2ecf20Sopenharmony_ci * Going to have to check what details I need to set and how to 2048c2ecf20Sopenharmony_ci * get them 2058c2ecf20Sopenharmony_ci */ 2068c2ecf20Sopenharmony_ci mtd->name = devm_kasprintf(dev, GFP_KERNEL, "%pOFP", dev->of_node); 2078c2ecf20Sopenharmony_ci mtd->type = MTD_NORFLASH; 2088c2ecf20Sopenharmony_ci mtd->flags = MTD_WRITEABLE; 2098c2ecf20Sopenharmony_ci mtd->size = size; 2108c2ecf20Sopenharmony_ci mtd->erasesize = erase_size; 2118c2ecf20Sopenharmony_ci mtd->writebufsize = mtd->writesize = 1; 2128c2ecf20Sopenharmony_ci mtd->owner = THIS_MODULE; 2138c2ecf20Sopenharmony_ci mtd->_erase = powernv_flash_erase; 2148c2ecf20Sopenharmony_ci mtd->_read = powernv_flash_read; 2158c2ecf20Sopenharmony_ci mtd->_write = powernv_flash_write; 2168c2ecf20Sopenharmony_ci mtd->dev.parent = dev; 2178c2ecf20Sopenharmony_ci mtd_set_of_node(mtd, dev->of_node); 2188c2ecf20Sopenharmony_ci return 0; 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci/** 2228c2ecf20Sopenharmony_ci * powernv_flash_probe 2238c2ecf20Sopenharmony_ci * @pdev: platform device 2248c2ecf20Sopenharmony_ci * 2258c2ecf20Sopenharmony_ci * Returns 0 on success, -ENOMEM, -ENXIO on error 2268c2ecf20Sopenharmony_ci */ 2278c2ecf20Sopenharmony_cistatic int powernv_flash_probe(struct platform_device *pdev) 2288c2ecf20Sopenharmony_ci{ 2298c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 2308c2ecf20Sopenharmony_ci struct powernv_flash *data; 2318c2ecf20Sopenharmony_ci int ret; 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 2348c2ecf20Sopenharmony_ci if (!data) 2358c2ecf20Sopenharmony_ci return -ENOMEM; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci data->mtd.priv = data; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci ret = of_property_read_u32(dev->of_node, "ibm,opal-id", &(data->id)); 2408c2ecf20Sopenharmony_ci if (ret) { 2418c2ecf20Sopenharmony_ci dev_err(dev, "no device property 'ibm,opal-id'\n"); 2428c2ecf20Sopenharmony_ci return ret; 2438c2ecf20Sopenharmony_ci } 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci ret = powernv_flash_set_driver_info(dev, &data->mtd); 2468c2ecf20Sopenharmony_ci if (ret) 2478c2ecf20Sopenharmony_ci return ret; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci dev_set_drvdata(dev, data); 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci /* 2528c2ecf20Sopenharmony_ci * The current flash that skiboot exposes is one contiguous flash chip 2538c2ecf20Sopenharmony_ci * with an ffs partition at the start, it should prove easier for users 2548c2ecf20Sopenharmony_ci * to deal with partitions or not as they see fit 2558c2ecf20Sopenharmony_ci */ 2568c2ecf20Sopenharmony_ci return mtd_device_register(&data->mtd, NULL, 0); 2578c2ecf20Sopenharmony_ci} 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci/** 2608c2ecf20Sopenharmony_ci * op_release - Release the driver 2618c2ecf20Sopenharmony_ci * @pdev: the platform device 2628c2ecf20Sopenharmony_ci * 2638c2ecf20Sopenharmony_ci * Returns 0 2648c2ecf20Sopenharmony_ci */ 2658c2ecf20Sopenharmony_cistatic int powernv_flash_release(struct platform_device *pdev) 2668c2ecf20Sopenharmony_ci{ 2678c2ecf20Sopenharmony_ci struct powernv_flash *data = dev_get_drvdata(&(pdev->dev)); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci /* All resources should be freed automatically */ 2708c2ecf20Sopenharmony_ci return mtd_device_unregister(&(data->mtd)); 2718c2ecf20Sopenharmony_ci} 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_cistatic const struct of_device_id powernv_flash_match[] = { 2748c2ecf20Sopenharmony_ci { .compatible = "ibm,opal-flash" }, 2758c2ecf20Sopenharmony_ci {} 2768c2ecf20Sopenharmony_ci}; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cistatic struct platform_driver powernv_flash_driver = { 2798c2ecf20Sopenharmony_ci .driver = { 2808c2ecf20Sopenharmony_ci .name = "powernv_flash", 2818c2ecf20Sopenharmony_ci .of_match_table = powernv_flash_match, 2828c2ecf20Sopenharmony_ci }, 2838c2ecf20Sopenharmony_ci .remove = powernv_flash_release, 2848c2ecf20Sopenharmony_ci .probe = powernv_flash_probe, 2858c2ecf20Sopenharmony_ci}; 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_cimodule_platform_driver(powernv_flash_driver); 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, powernv_flash_match); 2908c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 2918c2ecf20Sopenharmony_ciMODULE_AUTHOR("Cyril Bur <cyril.bur@au1.ibm.com>"); 2928c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MTD abstraction for OPAL flash"); 293