spi-wpcm-fiu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2022 Jonathan Neuschäfer
  3. #include <linux/clk.h>
  4. #include <linux/mfd/syscon.h>
  5. #include <linux/mod_devicetable.h>
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/regmap.h>
  9. #include <linux/spi/spi-mem.h>
  10. #define FIU_CFG 0x00
  11. #define FIU_BURST_BFG 0x01
  12. #define FIU_RESP_CFG 0x02
  13. #define FIU_CFBB_PROT 0x03
  14. #define FIU_FWIN1_LOW 0x04
  15. #define FIU_FWIN1_HIGH 0x06
  16. #define FIU_FWIN2_LOW 0x08
  17. #define FIU_FWIN2_HIGH 0x0a
  18. #define FIU_FWIN3_LOW 0x0c
  19. #define FIU_FWIN3_HIGH 0x0e
  20. #define FIU_PROT_LOCK 0x10
  21. #define FIU_PROT_CLEAR 0x11
  22. #define FIU_SPI_FL_CFG 0x14
  23. #define FIU_UMA_CODE 0x16
  24. #define FIU_UMA_AB0 0x17
  25. #define FIU_UMA_AB1 0x18
  26. #define FIU_UMA_AB2 0x19
  27. #define FIU_UMA_DB0 0x1a
  28. #define FIU_UMA_DB1 0x1b
  29. #define FIU_UMA_DB2 0x1c
  30. #define FIU_UMA_DB3 0x1d
  31. #define FIU_UMA_CTS 0x1e
  32. #define FIU_UMA_ECTS 0x1f
  33. #define FIU_BURST_CFG_R16 3
  34. #define FIU_UMA_CTS_D_SIZE(x) (x)
  35. #define FIU_UMA_CTS_A_SIZE BIT(3)
  36. #define FIU_UMA_CTS_WR BIT(4)
  37. #define FIU_UMA_CTS_CS(x) ((x) << 5)
  38. #define FIU_UMA_CTS_EXEC_DONE BIT(7)
  39. #define SHM_FLASH_SIZE 0x02
  40. #define SHM_FLASH_SIZE_STALL_HOST BIT(6)
  41. /*
  42. * I observed a typical wait time of 16 iterations for a UMA transfer to
  43. * finish, so this should be a safe limit.
  44. */
  45. #define UMA_WAIT_ITERATIONS 100
  46. /* The memory-mapped view of flash is 16 MiB long */
  47. #define MAX_MEMORY_SIZE_PER_CS (16 << 20)
  48. #define MAX_MEMORY_SIZE_TOTAL (4 * MAX_MEMORY_SIZE_PER_CS)
  49. struct wpcm_fiu_spi {
  50. struct device *dev;
  51. struct clk *clk;
  52. void __iomem *regs;
  53. void __iomem *memory;
  54. size_t memory_size;
  55. struct regmap *shm_regmap;
  56. };
  57. static void wpcm_fiu_set_opcode(struct wpcm_fiu_spi *fiu, u8 opcode)
  58. {
  59. writeb(opcode, fiu->regs + FIU_UMA_CODE);
  60. }
  61. static void wpcm_fiu_set_addr(struct wpcm_fiu_spi *fiu, u32 addr)
  62. {
  63. writeb((addr >> 0) & 0xff, fiu->regs + FIU_UMA_AB0);
  64. writeb((addr >> 8) & 0xff, fiu->regs + FIU_UMA_AB1);
  65. writeb((addr >> 16) & 0xff, fiu->regs + FIU_UMA_AB2);
  66. }
  67. static void wpcm_fiu_set_data(struct wpcm_fiu_spi *fiu, const u8 *data, unsigned int nbytes)
  68. {
  69. int i;
  70. for (i = 0; i < nbytes; i++)
  71. writeb(data[i], fiu->regs + FIU_UMA_DB0 + i);
  72. }
  73. static void wpcm_fiu_get_data(struct wpcm_fiu_spi *fiu, u8 *data, unsigned int nbytes)
  74. {
  75. int i;
  76. for (i = 0; i < nbytes; i++)
  77. data[i] = readb(fiu->regs + FIU_UMA_DB0 + i);
  78. }
  79. /*
  80. * Perform a UMA (User Mode Access) operation, i.e. a software-controlled SPI transfer.
  81. */
  82. static int wpcm_fiu_do_uma(struct wpcm_fiu_spi *fiu, unsigned int cs,
  83. bool use_addr, bool write, int data_bytes)
  84. {
  85. int i = 0;
  86. u8 cts = FIU_UMA_CTS_EXEC_DONE | FIU_UMA_CTS_CS(cs);
  87. if (use_addr)
  88. cts |= FIU_UMA_CTS_A_SIZE;
  89. if (write)
  90. cts |= FIU_UMA_CTS_WR;
  91. cts |= FIU_UMA_CTS_D_SIZE(data_bytes);
  92. writeb(cts, fiu->regs + FIU_UMA_CTS);
  93. for (i = 0; i < UMA_WAIT_ITERATIONS; i++)
  94. if (!(readb(fiu->regs + FIU_UMA_CTS) & FIU_UMA_CTS_EXEC_DONE))
  95. return 0;
  96. dev_info(fiu->dev, "UMA transfer has not finished in %d iterations\n", UMA_WAIT_ITERATIONS);
  97. return -EIO;
  98. }
  99. static void wpcm_fiu_ects_assert(struct wpcm_fiu_spi *fiu, unsigned int cs)
  100. {
  101. u8 ects = readb(fiu->regs + FIU_UMA_ECTS);
  102. ects &= ~BIT(cs);
  103. writeb(ects, fiu->regs + FIU_UMA_ECTS);
  104. }
  105. static void wpcm_fiu_ects_deassert(struct wpcm_fiu_spi *fiu, unsigned int cs)
  106. {
  107. u8 ects = readb(fiu->regs + FIU_UMA_ECTS);
  108. ects |= BIT(cs);
  109. writeb(ects, fiu->regs + FIU_UMA_ECTS);
  110. }
  111. struct wpcm_fiu_op_shape {
  112. bool (*match)(const struct spi_mem_op *op);
  113. int (*exec)(struct spi_mem *mem, const struct spi_mem_op *op);
  114. };
  115. static bool wpcm_fiu_normal_match(const struct spi_mem_op *op)
  116. {
  117. // Opcode 0x0b (FAST READ) is treated differently in hardware
  118. if (op->cmd.opcode == 0x0b)
  119. return false;
  120. return (op->addr.nbytes == 0 || op->addr.nbytes == 3) &&
  121. op->dummy.nbytes == 0 && op->data.nbytes <= 4;
  122. }
  123. static int wpcm_fiu_normal_exec(struct spi_mem *mem, const struct spi_mem_op *op)
  124. {
  125. struct wpcm_fiu_spi *fiu = spi_controller_get_devdata(mem->spi->controller);
  126. int ret;
  127. wpcm_fiu_set_opcode(fiu, op->cmd.opcode);
  128. wpcm_fiu_set_addr(fiu, op->addr.val);
  129. if (op->data.dir == SPI_MEM_DATA_OUT)
  130. wpcm_fiu_set_data(fiu, op->data.buf.out, op->data.nbytes);
  131. ret = wpcm_fiu_do_uma(fiu, spi_get_chipselect(mem->spi, 0), op->addr.nbytes == 3,
  132. op->data.dir == SPI_MEM_DATA_OUT, op->data.nbytes);
  133. if (op->data.dir == SPI_MEM_DATA_IN)
  134. wpcm_fiu_get_data(fiu, op->data.buf.in, op->data.nbytes);
  135. return ret;
  136. }
  137. static bool wpcm_fiu_fast_read_match(const struct spi_mem_op *op)
  138. {
  139. return op->cmd.opcode == 0x0b && op->addr.nbytes == 3 &&
  140. op->dummy.nbytes == 1 &&
  141. op->data.nbytes >= 1 && op->data.nbytes <= 4 &&
  142. op->data.dir == SPI_MEM_DATA_IN;
  143. }
  144. static int wpcm_fiu_fast_read_exec(struct spi_mem *mem, const struct spi_mem_op *op)
  145. {
  146. return -EINVAL;
  147. }
  148. /*
  149. * 4-byte addressing.
  150. *
  151. * Flash view: [ C A A A A D D D D]
  152. * bytes: 13 aa bb cc dd -> 5a a5 f0 0f
  153. * FIU's view: [ C A A A][ C D D D D]
  154. * FIU mode: [ read/write][ read ]
  155. */
  156. static bool wpcm_fiu_4ba_match(const struct spi_mem_op *op)
  157. {
  158. return op->addr.nbytes == 4 && op->dummy.nbytes == 0 && op->data.nbytes <= 4;
  159. }
  160. static int wpcm_fiu_4ba_exec(struct spi_mem *mem, const struct spi_mem_op *op)
  161. {
  162. struct wpcm_fiu_spi *fiu = spi_controller_get_devdata(mem->spi->controller);
  163. int cs = spi_get_chipselect(mem->spi, 0);
  164. wpcm_fiu_ects_assert(fiu, cs);
  165. wpcm_fiu_set_opcode(fiu, op->cmd.opcode);
  166. wpcm_fiu_set_addr(fiu, op->addr.val >> 8);
  167. wpcm_fiu_do_uma(fiu, cs, true, false, 0);
  168. wpcm_fiu_set_opcode(fiu, op->addr.val & 0xff);
  169. wpcm_fiu_set_addr(fiu, 0);
  170. if (op->data.dir == SPI_MEM_DATA_OUT)
  171. wpcm_fiu_set_data(fiu, op->data.buf.out, op->data.nbytes);
  172. wpcm_fiu_do_uma(fiu, cs, false, op->data.dir == SPI_MEM_DATA_OUT, op->data.nbytes);
  173. wpcm_fiu_ects_deassert(fiu, cs);
  174. if (op->data.dir == SPI_MEM_DATA_IN)
  175. wpcm_fiu_get_data(fiu, op->data.buf.in, op->data.nbytes);
  176. return 0;
  177. }
  178. /*
  179. * RDID (Read Identification) needs special handling because Linux expects to
  180. * be able to read 6 ID bytes and FIU can only read up to 4 at once.
  181. *
  182. * We're lucky in this case, because executing the RDID instruction twice will
  183. * result in the same result.
  184. *
  185. * What we do is as follows (C: write command/opcode byte, D: read data byte,
  186. * A: write address byte):
  187. *
  188. * 1. C D D D
  189. * 2. C A A A D D D
  190. */
  191. static bool wpcm_fiu_rdid_match(const struct spi_mem_op *op)
  192. {
  193. return op->cmd.opcode == 0x9f && op->addr.nbytes == 0 &&
  194. op->dummy.nbytes == 0 && op->data.nbytes == 6 &&
  195. op->data.dir == SPI_MEM_DATA_IN;
  196. }
  197. static int wpcm_fiu_rdid_exec(struct spi_mem *mem, const struct spi_mem_op *op)
  198. {
  199. struct wpcm_fiu_spi *fiu = spi_controller_get_devdata(mem->spi->controller);
  200. int cs = spi_get_chipselect(mem->spi, 0);
  201. /* First transfer */
  202. wpcm_fiu_set_opcode(fiu, op->cmd.opcode);
  203. wpcm_fiu_set_addr(fiu, 0);
  204. wpcm_fiu_do_uma(fiu, cs, false, false, 3);
  205. wpcm_fiu_get_data(fiu, op->data.buf.in, 3);
  206. /* Second transfer */
  207. wpcm_fiu_set_opcode(fiu, op->cmd.opcode);
  208. wpcm_fiu_set_addr(fiu, 0);
  209. wpcm_fiu_do_uma(fiu, cs, true, false, 3);
  210. wpcm_fiu_get_data(fiu, op->data.buf.in + 3, 3);
  211. return 0;
  212. }
  213. /*
  214. * With some dummy bytes.
  215. *
  216. * C A A A X* X D D D D
  217. * [C A A A D*][C D D D D]
  218. */
  219. static bool wpcm_fiu_dummy_match(const struct spi_mem_op *op)
  220. {
  221. // Opcode 0x0b (FAST READ) is treated differently in hardware
  222. if (op->cmd.opcode == 0x0b)
  223. return false;
  224. return (op->addr.nbytes == 0 || op->addr.nbytes == 3) &&
  225. op->dummy.nbytes >= 1 && op->dummy.nbytes <= 5 &&
  226. op->data.nbytes <= 4;
  227. }
  228. static int wpcm_fiu_dummy_exec(struct spi_mem *mem, const struct spi_mem_op *op)
  229. {
  230. struct wpcm_fiu_spi *fiu = spi_controller_get_devdata(mem->spi->controller);
  231. int cs = spi_get_chipselect(mem->spi, 0);
  232. wpcm_fiu_ects_assert(fiu, cs);
  233. /* First transfer */
  234. wpcm_fiu_set_opcode(fiu, op->cmd.opcode);
  235. wpcm_fiu_set_addr(fiu, op->addr.val);
  236. wpcm_fiu_do_uma(fiu, cs, op->addr.nbytes != 0, true, op->dummy.nbytes - 1);
  237. /* Second transfer */
  238. wpcm_fiu_set_opcode(fiu, 0);
  239. wpcm_fiu_set_addr(fiu, 0);
  240. wpcm_fiu_do_uma(fiu, cs, false, false, op->data.nbytes);
  241. wpcm_fiu_get_data(fiu, op->data.buf.in, op->data.nbytes);
  242. wpcm_fiu_ects_deassert(fiu, cs);
  243. return 0;
  244. }
  245. static const struct wpcm_fiu_op_shape wpcm_fiu_op_shapes[] = {
  246. { .match = wpcm_fiu_normal_match, .exec = wpcm_fiu_normal_exec },
  247. { .match = wpcm_fiu_fast_read_match, .exec = wpcm_fiu_fast_read_exec },
  248. { .match = wpcm_fiu_4ba_match, .exec = wpcm_fiu_4ba_exec },
  249. { .match = wpcm_fiu_rdid_match, .exec = wpcm_fiu_rdid_exec },
  250. { .match = wpcm_fiu_dummy_match, .exec = wpcm_fiu_dummy_exec },
  251. };
  252. static const struct wpcm_fiu_op_shape *wpcm_fiu_find_op_shape(const struct spi_mem_op *op)
  253. {
  254. size_t i;
  255. for (i = 0; i < ARRAY_SIZE(wpcm_fiu_op_shapes); i++) {
  256. const struct wpcm_fiu_op_shape *shape = &wpcm_fiu_op_shapes[i];
  257. if (shape->match(op))
  258. return shape;
  259. }
  260. return NULL;
  261. }
  262. static bool wpcm_fiu_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
  263. {
  264. if (!spi_mem_default_supports_op(mem, op))
  265. return false;
  266. if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr)
  267. return false;
  268. if (op->cmd.buswidth > 1 || op->addr.buswidth > 1 ||
  269. op->dummy.buswidth > 1 || op->data.buswidth > 1)
  270. return false;
  271. return wpcm_fiu_find_op_shape(op) != NULL;
  272. }
  273. /*
  274. * In order to ensure the integrity of SPI transfers performed via UMA,
  275. * temporarily disable (stall) memory accesses coming from the host CPU.
  276. */
  277. static void wpcm_fiu_stall_host(struct wpcm_fiu_spi *fiu, bool stall)
  278. {
  279. if (fiu->shm_regmap) {
  280. int res = regmap_update_bits(fiu->shm_regmap, SHM_FLASH_SIZE,
  281. SHM_FLASH_SIZE_STALL_HOST,
  282. stall ? SHM_FLASH_SIZE_STALL_HOST : 0);
  283. if (res)
  284. dev_warn(fiu->dev, "Failed to (un)stall host memory accesses: %d\n", res);
  285. }
  286. }
  287. static int wpcm_fiu_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
  288. {
  289. struct wpcm_fiu_spi *fiu = spi_controller_get_devdata(mem->spi->controller);
  290. const struct wpcm_fiu_op_shape *shape = wpcm_fiu_find_op_shape(op);
  291. wpcm_fiu_stall_host(fiu, true);
  292. if (shape)
  293. return shape->exec(mem, op);
  294. wpcm_fiu_stall_host(fiu, false);
  295. return -EOPNOTSUPP;
  296. }
  297. static int wpcm_fiu_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
  298. {
  299. if (op->data.nbytes > 4)
  300. op->data.nbytes = 4;
  301. return 0;
  302. }
  303. static int wpcm_fiu_dirmap_create(struct spi_mem_dirmap_desc *desc)
  304. {
  305. struct wpcm_fiu_spi *fiu = spi_controller_get_devdata(desc->mem->spi->controller);
  306. int cs = spi_get_chipselect(desc->mem->spi, 0);
  307. if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN)
  308. return -EOPNOTSUPP;
  309. /*
  310. * Unfortunately, FIU only supports a 16 MiB direct mapping window (per
  311. * attached flash chip), but the SPI MEM core doesn't support partial
  312. * direct mappings. This means that we can't support direct mapping on
  313. * flashes that are bigger than 16 MiB.
  314. */
  315. if (desc->info.offset + desc->info.length > MAX_MEMORY_SIZE_PER_CS)
  316. return -EINVAL;
  317. /* Don't read past the memory window */
  318. if (cs * MAX_MEMORY_SIZE_PER_CS + desc->info.offset + desc->info.length > fiu->memory_size)
  319. return -EINVAL;
  320. return 0;
  321. }
  322. static ssize_t wpcm_fiu_direct_read(struct spi_mem_dirmap_desc *desc, u64 offs, size_t len, void *buf)
  323. {
  324. struct wpcm_fiu_spi *fiu = spi_controller_get_devdata(desc->mem->spi->controller);
  325. int cs = spi_get_chipselect(desc->mem->spi, 0);
  326. if (offs >= MAX_MEMORY_SIZE_PER_CS)
  327. return -ENOTSUPP;
  328. offs += cs * MAX_MEMORY_SIZE_PER_CS;
  329. if (!fiu->memory || offs >= fiu->memory_size)
  330. return -ENOTSUPP;
  331. len = min_t(size_t, len, fiu->memory_size - offs);
  332. memcpy_fromio(buf, fiu->memory + offs, len);
  333. return len;
  334. }
  335. static const struct spi_controller_mem_ops wpcm_fiu_mem_ops = {
  336. .adjust_op_size = wpcm_fiu_adjust_op_size,
  337. .supports_op = wpcm_fiu_supports_op,
  338. .exec_op = wpcm_fiu_exec_op,
  339. .dirmap_create = wpcm_fiu_dirmap_create,
  340. .dirmap_read = wpcm_fiu_direct_read,
  341. };
  342. static void wpcm_fiu_hw_init(struct wpcm_fiu_spi *fiu)
  343. {
  344. /* Configure memory-mapped flash access */
  345. writeb(FIU_BURST_CFG_R16, fiu->regs + FIU_BURST_BFG);
  346. writeb(MAX_MEMORY_SIZE_TOTAL / (512 << 10), fiu->regs + FIU_CFG);
  347. writeb(MAX_MEMORY_SIZE_PER_CS / (512 << 10) | BIT(6), fiu->regs + FIU_SPI_FL_CFG);
  348. /* Deassert all manually asserted chip selects */
  349. writeb(0x0f, fiu->regs + FIU_UMA_ECTS);
  350. }
  351. static int wpcm_fiu_probe(struct platform_device *pdev)
  352. {
  353. struct device *dev = &pdev->dev;
  354. struct spi_controller *ctrl;
  355. struct wpcm_fiu_spi *fiu;
  356. struct resource *res;
  357. ctrl = devm_spi_alloc_host(dev, sizeof(*fiu));
  358. if (!ctrl)
  359. return -ENOMEM;
  360. fiu = spi_controller_get_devdata(ctrl);
  361. fiu->dev = dev;
  362. fiu->regs = devm_platform_ioremap_resource_byname(pdev, "control");
  363. if (IS_ERR(fiu->regs))
  364. return dev_err_probe(dev, PTR_ERR(fiu->regs),
  365. "Failed to map registers\n");
  366. fiu->clk = devm_clk_get_enabled(dev, NULL);
  367. if (IS_ERR(fiu->clk))
  368. return PTR_ERR(fiu->clk);
  369. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "memory");
  370. fiu->memory = devm_ioremap_resource(dev, res);
  371. fiu->memory_size = min_t(size_t, resource_size(res), MAX_MEMORY_SIZE_TOTAL);
  372. if (IS_ERR(fiu->memory))
  373. return dev_err_probe(dev, PTR_ERR(fiu->memory),
  374. "Failed to map flash memory window\n");
  375. fiu->shm_regmap = syscon_regmap_lookup_by_phandle_optional(dev->of_node, "nuvoton,shm");
  376. wpcm_fiu_hw_init(fiu);
  377. ctrl->bus_num = -1;
  378. ctrl->mem_ops = &wpcm_fiu_mem_ops;
  379. ctrl->num_chipselect = 4;
  380. ctrl->dev.of_node = dev->of_node;
  381. /*
  382. * The FIU doesn't include a clock divider, the clock is entirely
  383. * determined by the AHB3 bus clock.
  384. */
  385. ctrl->min_speed_hz = clk_get_rate(fiu->clk);
  386. ctrl->max_speed_hz = clk_get_rate(fiu->clk);
  387. return devm_spi_register_controller(dev, ctrl);
  388. }
  389. static const struct of_device_id wpcm_fiu_dt_ids[] = {
  390. { .compatible = "nuvoton,wpcm450-fiu", },
  391. { }
  392. };
  393. MODULE_DEVICE_TABLE(of, wpcm_fiu_dt_ids);
  394. static struct platform_driver wpcm_fiu_driver = {
  395. .driver = {
  396. .name = "wpcm450-fiu",
  397. .bus = &platform_bus_type,
  398. .of_match_table = wpcm_fiu_dt_ids,
  399. },
  400. .probe = wpcm_fiu_probe,
  401. };
  402. module_platform_driver(wpcm_fiu_driver);
  403. MODULE_DESCRIPTION("Nuvoton WPCM450 FIU SPI controller driver");
  404. MODULE_AUTHOR("Jonathan Neuschäfer <j.neuschaefer@gmx.net>");
  405. MODULE_LICENSE("GPL");