stm32-quadspi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Driver for stm32 quadspi controller
  3. *
  4. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  5. * Author(s): Ludovic Barre author <ludovic.barre@st.com>.
  6. *
  7. * License terms: GPL V2.0.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * This program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/errno.h>
  23. #include <linux/io.h>
  24. #include <linux/iopoll.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/mtd/partitions.h>
  29. #include <linux/mtd/spi-nor.h>
  30. #include <linux/mutex.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/reset.h>
  35. #include <linux/sizes.h>
  36. #define QUADSPI_CR 0x00
  37. #define CR_EN BIT(0)
  38. #define CR_ABORT BIT(1)
  39. #define CR_DMAEN BIT(2)
  40. #define CR_TCEN BIT(3)
  41. #define CR_SSHIFT BIT(4)
  42. #define CR_DFM BIT(6)
  43. #define CR_FSEL BIT(7)
  44. #define CR_FTHRES_SHIFT 8
  45. #define CR_FTHRES_MASK GENMASK(12, 8)
  46. #define CR_FTHRES(n) (((n) << CR_FTHRES_SHIFT) & CR_FTHRES_MASK)
  47. #define CR_TEIE BIT(16)
  48. #define CR_TCIE BIT(17)
  49. #define CR_FTIE BIT(18)
  50. #define CR_SMIE BIT(19)
  51. #define CR_TOIE BIT(20)
  52. #define CR_PRESC_SHIFT 24
  53. #define CR_PRESC_MASK GENMASK(31, 24)
  54. #define CR_PRESC(n) (((n) << CR_PRESC_SHIFT) & CR_PRESC_MASK)
  55. #define QUADSPI_DCR 0x04
  56. #define DCR_CSHT_SHIFT 8
  57. #define DCR_CSHT_MASK GENMASK(10, 8)
  58. #define DCR_CSHT(n) (((n) << DCR_CSHT_SHIFT) & DCR_CSHT_MASK)
  59. #define DCR_FSIZE_SHIFT 16
  60. #define DCR_FSIZE_MASK GENMASK(20, 16)
  61. #define DCR_FSIZE(n) (((n) << DCR_FSIZE_SHIFT) & DCR_FSIZE_MASK)
  62. #define QUADSPI_SR 0x08
  63. #define SR_TEF BIT(0)
  64. #define SR_TCF BIT(1)
  65. #define SR_FTF BIT(2)
  66. #define SR_SMF BIT(3)
  67. #define SR_TOF BIT(4)
  68. #define SR_BUSY BIT(5)
  69. #define SR_FLEVEL_SHIFT 8
  70. #define SR_FLEVEL_MASK GENMASK(13, 8)
  71. #define QUADSPI_FCR 0x0c
  72. #define FCR_CTCF BIT(1)
  73. #define QUADSPI_DLR 0x10
  74. #define QUADSPI_CCR 0x14
  75. #define CCR_INST_SHIFT 0
  76. #define CCR_INST_MASK GENMASK(7, 0)
  77. #define CCR_INST(n) (((n) << CCR_INST_SHIFT) & CCR_INST_MASK)
  78. #define CCR_IMODE_NONE (0U << 8)
  79. #define CCR_IMODE_1 (1U << 8)
  80. #define CCR_IMODE_2 (2U << 8)
  81. #define CCR_IMODE_4 (3U << 8)
  82. #define CCR_ADMODE_NONE (0U << 10)
  83. #define CCR_ADMODE_1 (1U << 10)
  84. #define CCR_ADMODE_2 (2U << 10)
  85. #define CCR_ADMODE_4 (3U << 10)
  86. #define CCR_ADSIZE_SHIFT 12
  87. #define CCR_ADSIZE_MASK GENMASK(13, 12)
  88. #define CCR_ADSIZE(n) (((n) << CCR_ADSIZE_SHIFT) & CCR_ADSIZE_MASK)
  89. #define CCR_ABMODE_NONE (0U << 14)
  90. #define CCR_ABMODE_1 (1U << 14)
  91. #define CCR_ABMODE_2 (2U << 14)
  92. #define CCR_ABMODE_4 (3U << 14)
  93. #define CCR_ABSIZE_8 (0U << 16)
  94. #define CCR_ABSIZE_16 (1U << 16)
  95. #define CCR_ABSIZE_24 (2U << 16)
  96. #define CCR_ABSIZE_32 (3U << 16)
  97. #define CCR_DCYC_SHIFT 18
  98. #define CCR_DCYC_MASK GENMASK(22, 18)
  99. #define CCR_DCYC(n) (((n) << CCR_DCYC_SHIFT) & CCR_DCYC_MASK)
  100. #define CCR_DMODE_NONE (0U << 24)
  101. #define CCR_DMODE_1 (1U << 24)
  102. #define CCR_DMODE_2 (2U << 24)
  103. #define CCR_DMODE_4 (3U << 24)
  104. #define CCR_FMODE_INDW (0U << 26)
  105. #define CCR_FMODE_INDR (1U << 26)
  106. #define CCR_FMODE_APM (2U << 26)
  107. #define CCR_FMODE_MM (3U << 26)
  108. #define QUADSPI_AR 0x18
  109. #define QUADSPI_ABR 0x1c
  110. #define QUADSPI_DR 0x20
  111. #define QUADSPI_PSMKR 0x24
  112. #define QUADSPI_PSMAR 0x28
  113. #define QUADSPI_PIR 0x2c
  114. #define QUADSPI_LPTR 0x30
  115. #define LPTR_DFT_TIMEOUT 0x10
  116. #define FSIZE_VAL(size) (__fls(size) - 1)
  117. #define STM32_MAX_MMAP_SZ SZ_256M
  118. #define STM32_MAX_NORCHIP 2
  119. #define STM32_QSPI_FIFO_SZ 32
  120. #define STM32_QSPI_FIFO_TIMEOUT_US 30000
  121. #define STM32_QSPI_BUSY_TIMEOUT_US 100000
  122. struct stm32_qspi_flash {
  123. struct spi_nor nor;
  124. struct stm32_qspi *qspi;
  125. u32 cs;
  126. u32 fsize;
  127. u32 presc;
  128. u32 read_mode;
  129. bool registered;
  130. u32 prefetch_limit;
  131. };
  132. struct stm32_qspi {
  133. struct device *dev;
  134. void __iomem *io_base;
  135. void __iomem *mm_base;
  136. resource_size_t mm_size;
  137. u32 nor_num;
  138. struct clk *clk;
  139. u32 clk_rate;
  140. struct stm32_qspi_flash flash[STM32_MAX_NORCHIP];
  141. struct completion cmd_completion;
  142. /*
  143. * to protect device configuration, could be different between
  144. * 2 flash access (bk1, bk2)
  145. */
  146. struct mutex lock;
  147. };
  148. struct stm32_qspi_cmd {
  149. u8 addr_width;
  150. u8 dummy;
  151. bool tx_data;
  152. u8 opcode;
  153. u32 framemode;
  154. u32 qspimode;
  155. u32 addr;
  156. size_t len;
  157. void *buf;
  158. };
  159. static int stm32_qspi_wait_cmd(struct stm32_qspi *qspi)
  160. {
  161. u32 cr;
  162. int err = 0;
  163. if (readl_relaxed(qspi->io_base + QUADSPI_SR) & SR_TCF)
  164. return 0;
  165. reinit_completion(&qspi->cmd_completion);
  166. cr = readl_relaxed(qspi->io_base + QUADSPI_CR);
  167. writel_relaxed(cr | CR_TCIE, qspi->io_base + QUADSPI_CR);
  168. if (!wait_for_completion_interruptible_timeout(&qspi->cmd_completion,
  169. msecs_to_jiffies(1000)))
  170. err = -ETIMEDOUT;
  171. writel_relaxed(cr, qspi->io_base + QUADSPI_CR);
  172. return err;
  173. }
  174. static int stm32_qspi_wait_nobusy(struct stm32_qspi *qspi)
  175. {
  176. u32 sr;
  177. return readl_relaxed_poll_timeout(qspi->io_base + QUADSPI_SR, sr,
  178. !(sr & SR_BUSY), 10,
  179. STM32_QSPI_BUSY_TIMEOUT_US);
  180. }
  181. static void stm32_qspi_set_framemode(struct spi_nor *nor,
  182. struct stm32_qspi_cmd *cmd, bool read)
  183. {
  184. u32 dmode = CCR_DMODE_1;
  185. cmd->framemode = CCR_IMODE_1;
  186. if (read) {
  187. switch (nor->read_proto) {
  188. default:
  189. case SNOR_PROTO_1_1_1:
  190. dmode = CCR_DMODE_1;
  191. break;
  192. case SNOR_PROTO_1_1_2:
  193. dmode = CCR_DMODE_2;
  194. break;
  195. case SNOR_PROTO_1_1_4:
  196. dmode = CCR_DMODE_4;
  197. break;
  198. }
  199. }
  200. cmd->framemode |= cmd->tx_data ? dmode : 0;
  201. cmd->framemode |= cmd->addr_width ? CCR_ADMODE_1 : 0;
  202. }
  203. static void stm32_qspi_read_fifo(u8 *val, void __iomem *addr)
  204. {
  205. *val = readb_relaxed(addr);
  206. }
  207. static void stm32_qspi_write_fifo(u8 *val, void __iomem *addr)
  208. {
  209. writeb_relaxed(*val, addr);
  210. }
  211. static int stm32_qspi_tx_poll(struct stm32_qspi *qspi,
  212. const struct stm32_qspi_cmd *cmd)
  213. {
  214. void (*tx_fifo)(u8 *, void __iomem *);
  215. u32 len = cmd->len, sr;
  216. u8 *buf = cmd->buf;
  217. int ret;
  218. if (cmd->qspimode == CCR_FMODE_INDW)
  219. tx_fifo = stm32_qspi_write_fifo;
  220. else
  221. tx_fifo = stm32_qspi_read_fifo;
  222. while (len--) {
  223. ret = readl_relaxed_poll_timeout(qspi->io_base + QUADSPI_SR,
  224. sr, (sr & SR_FTF), 10,
  225. STM32_QSPI_FIFO_TIMEOUT_US);
  226. if (ret) {
  227. dev_err(qspi->dev, "fifo timeout (stat:%#x)\n", sr);
  228. return ret;
  229. }
  230. tx_fifo(buf++, qspi->io_base + QUADSPI_DR);
  231. }
  232. return 0;
  233. }
  234. static int stm32_qspi_tx_mm(struct stm32_qspi *qspi,
  235. const struct stm32_qspi_cmd *cmd)
  236. {
  237. memcpy_fromio(cmd->buf, qspi->mm_base + cmd->addr, cmd->len);
  238. return 0;
  239. }
  240. static int stm32_qspi_tx(struct stm32_qspi *qspi,
  241. const struct stm32_qspi_cmd *cmd)
  242. {
  243. if (!cmd->tx_data)
  244. return 0;
  245. if (cmd->qspimode == CCR_FMODE_MM)
  246. return stm32_qspi_tx_mm(qspi, cmd);
  247. return stm32_qspi_tx_poll(qspi, cmd);
  248. }
  249. static int stm32_qspi_send(struct stm32_qspi_flash *flash,
  250. const struct stm32_qspi_cmd *cmd)
  251. {
  252. struct stm32_qspi *qspi = flash->qspi;
  253. u32 ccr, dcr, cr;
  254. u32 last_byte;
  255. int err;
  256. err = stm32_qspi_wait_nobusy(qspi);
  257. if (err)
  258. goto abort;
  259. dcr = readl_relaxed(qspi->io_base + QUADSPI_DCR) & ~DCR_FSIZE_MASK;
  260. dcr |= DCR_FSIZE(flash->fsize);
  261. writel_relaxed(dcr, qspi->io_base + QUADSPI_DCR);
  262. cr = readl_relaxed(qspi->io_base + QUADSPI_CR);
  263. cr &= ~CR_PRESC_MASK & ~CR_FSEL;
  264. cr |= CR_PRESC(flash->presc);
  265. cr |= flash->cs ? CR_FSEL : 0;
  266. writel_relaxed(cr, qspi->io_base + QUADSPI_CR);
  267. if (cmd->tx_data)
  268. writel_relaxed(cmd->len - 1, qspi->io_base + QUADSPI_DLR);
  269. ccr = cmd->framemode | cmd->qspimode;
  270. if (cmd->dummy)
  271. ccr |= CCR_DCYC(cmd->dummy);
  272. if (cmd->addr_width)
  273. ccr |= CCR_ADSIZE(cmd->addr_width - 1);
  274. ccr |= CCR_INST(cmd->opcode);
  275. writel_relaxed(ccr, qspi->io_base + QUADSPI_CCR);
  276. if (cmd->addr_width && cmd->qspimode != CCR_FMODE_MM)
  277. writel_relaxed(cmd->addr, qspi->io_base + QUADSPI_AR);
  278. err = stm32_qspi_tx(qspi, cmd);
  279. if (err)
  280. goto abort;
  281. if (cmd->qspimode != CCR_FMODE_MM) {
  282. err = stm32_qspi_wait_cmd(qspi);
  283. if (err)
  284. goto abort;
  285. writel_relaxed(FCR_CTCF, qspi->io_base + QUADSPI_FCR);
  286. } else {
  287. last_byte = cmd->addr + cmd->len;
  288. if (last_byte > flash->prefetch_limit)
  289. goto abort;
  290. }
  291. return err;
  292. abort:
  293. cr = readl_relaxed(qspi->io_base + QUADSPI_CR) | CR_ABORT;
  294. writel_relaxed(cr, qspi->io_base + QUADSPI_CR);
  295. if (err)
  296. dev_err(qspi->dev, "%s abort err:%d\n", __func__, err);
  297. return err;
  298. }
  299. static int stm32_qspi_read_reg(struct spi_nor *nor,
  300. u8 opcode, u8 *buf, int len)
  301. {
  302. struct stm32_qspi_flash *flash = nor->priv;
  303. struct device *dev = flash->qspi->dev;
  304. struct stm32_qspi_cmd cmd;
  305. dev_dbg(dev, "read_reg: cmd:%#.2x buf:%pK len:%#x\n", opcode, buf, len);
  306. memset(&cmd, 0, sizeof(cmd));
  307. cmd.opcode = opcode;
  308. cmd.tx_data = true;
  309. cmd.len = len;
  310. cmd.buf = buf;
  311. cmd.qspimode = CCR_FMODE_INDR;
  312. stm32_qspi_set_framemode(nor, &cmd, false);
  313. return stm32_qspi_send(flash, &cmd);
  314. }
  315. static int stm32_qspi_write_reg(struct spi_nor *nor, u8 opcode,
  316. u8 *buf, int len)
  317. {
  318. struct stm32_qspi_flash *flash = nor->priv;
  319. struct device *dev = flash->qspi->dev;
  320. struct stm32_qspi_cmd cmd;
  321. dev_dbg(dev, "write_reg: cmd:%#.2x buf:%pK len:%#x\n", opcode, buf, len);
  322. memset(&cmd, 0, sizeof(cmd));
  323. cmd.opcode = opcode;
  324. cmd.tx_data = !!(buf && len > 0);
  325. cmd.len = len;
  326. cmd.buf = buf;
  327. cmd.qspimode = CCR_FMODE_INDW;
  328. stm32_qspi_set_framemode(nor, &cmd, false);
  329. return stm32_qspi_send(flash, &cmd);
  330. }
  331. static ssize_t stm32_qspi_read(struct spi_nor *nor, loff_t from, size_t len,
  332. u_char *buf)
  333. {
  334. struct stm32_qspi_flash *flash = nor->priv;
  335. struct stm32_qspi *qspi = flash->qspi;
  336. struct stm32_qspi_cmd cmd;
  337. int err;
  338. dev_dbg(qspi->dev, "read(%#.2x): buf:%pK from:%#.8x len:%#zx\n",
  339. nor->read_opcode, buf, (u32)from, len);
  340. memset(&cmd, 0, sizeof(cmd));
  341. cmd.opcode = nor->read_opcode;
  342. cmd.addr_width = nor->addr_width;
  343. cmd.addr = (u32)from;
  344. cmd.tx_data = true;
  345. cmd.dummy = nor->read_dummy;
  346. cmd.len = len;
  347. cmd.buf = buf;
  348. cmd.qspimode = flash->read_mode;
  349. stm32_qspi_set_framemode(nor, &cmd, true);
  350. err = stm32_qspi_send(flash, &cmd);
  351. return err ? err : len;
  352. }
  353. static ssize_t stm32_qspi_write(struct spi_nor *nor, loff_t to, size_t len,
  354. const u_char *buf)
  355. {
  356. struct stm32_qspi_flash *flash = nor->priv;
  357. struct device *dev = flash->qspi->dev;
  358. struct stm32_qspi_cmd cmd;
  359. int err;
  360. dev_dbg(dev, "write(%#.2x): buf:%p to:%#.8x len:%#zx\n",
  361. nor->program_opcode, buf, (u32)to, len);
  362. memset(&cmd, 0, sizeof(cmd));
  363. cmd.opcode = nor->program_opcode;
  364. cmd.addr_width = nor->addr_width;
  365. cmd.addr = (u32)to;
  366. cmd.tx_data = true;
  367. cmd.len = len;
  368. cmd.buf = (void *)buf;
  369. cmd.qspimode = CCR_FMODE_INDW;
  370. stm32_qspi_set_framemode(nor, &cmd, false);
  371. err = stm32_qspi_send(flash, &cmd);
  372. return err ? err : len;
  373. }
  374. static int stm32_qspi_erase(struct spi_nor *nor, loff_t offs)
  375. {
  376. struct stm32_qspi_flash *flash = nor->priv;
  377. struct device *dev = flash->qspi->dev;
  378. struct stm32_qspi_cmd cmd;
  379. dev_dbg(dev, "erase(%#.2x):offs:%#x\n", nor->erase_opcode, (u32)offs);
  380. memset(&cmd, 0, sizeof(cmd));
  381. cmd.opcode = nor->erase_opcode;
  382. cmd.addr_width = nor->addr_width;
  383. cmd.addr = (u32)offs;
  384. cmd.qspimode = CCR_FMODE_INDW;
  385. stm32_qspi_set_framemode(nor, &cmd, false);
  386. return stm32_qspi_send(flash, &cmd);
  387. }
  388. static irqreturn_t stm32_qspi_irq(int irq, void *dev_id)
  389. {
  390. struct stm32_qspi *qspi = (struct stm32_qspi *)dev_id;
  391. u32 cr, sr, fcr = 0;
  392. cr = readl_relaxed(qspi->io_base + QUADSPI_CR);
  393. sr = readl_relaxed(qspi->io_base + QUADSPI_SR);
  394. if ((cr & CR_TCIE) && (sr & SR_TCF)) {
  395. /* tx complete */
  396. fcr |= FCR_CTCF;
  397. complete(&qspi->cmd_completion);
  398. } else {
  399. dev_info_ratelimited(qspi->dev, "spurious interrupt\n");
  400. }
  401. writel_relaxed(fcr, qspi->io_base + QUADSPI_FCR);
  402. return IRQ_HANDLED;
  403. }
  404. static int stm32_qspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  405. {
  406. struct stm32_qspi_flash *flash = nor->priv;
  407. struct stm32_qspi *qspi = flash->qspi;
  408. mutex_lock(&qspi->lock);
  409. return 0;
  410. }
  411. static void stm32_qspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  412. {
  413. struct stm32_qspi_flash *flash = nor->priv;
  414. struct stm32_qspi *qspi = flash->qspi;
  415. mutex_unlock(&qspi->lock);
  416. }
  417. static int stm32_qspi_flash_setup(struct stm32_qspi *qspi,
  418. struct device_node *np)
  419. {
  420. struct spi_nor_hwcaps hwcaps = {
  421. .mask = SNOR_HWCAPS_READ |
  422. SNOR_HWCAPS_READ_FAST |
  423. SNOR_HWCAPS_PP,
  424. };
  425. u32 width, presc, cs_num, max_rate = 0;
  426. struct stm32_qspi_flash *flash;
  427. struct mtd_info *mtd;
  428. int ret;
  429. of_property_read_u32(np, "reg", &cs_num);
  430. if (cs_num >= STM32_MAX_NORCHIP)
  431. return -EINVAL;
  432. of_property_read_u32(np, "spi-max-frequency", &max_rate);
  433. if (!max_rate)
  434. return -EINVAL;
  435. presc = DIV_ROUND_UP(qspi->clk_rate, max_rate) - 1;
  436. if (of_property_read_u32(np, "spi-rx-bus-width", &width))
  437. width = 1;
  438. if (width == 4)
  439. hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
  440. else if (width == 2)
  441. hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
  442. else if (width != 1)
  443. return -EINVAL;
  444. flash = &qspi->flash[cs_num];
  445. flash->qspi = qspi;
  446. flash->cs = cs_num;
  447. flash->presc = presc;
  448. flash->nor.dev = qspi->dev;
  449. spi_nor_set_flash_node(&flash->nor, np);
  450. flash->nor.priv = flash;
  451. mtd = &flash->nor.mtd;
  452. flash->nor.read = stm32_qspi_read;
  453. flash->nor.write = stm32_qspi_write;
  454. flash->nor.erase = stm32_qspi_erase;
  455. flash->nor.read_reg = stm32_qspi_read_reg;
  456. flash->nor.write_reg = stm32_qspi_write_reg;
  457. flash->nor.prepare = stm32_qspi_prep;
  458. flash->nor.unprepare = stm32_qspi_unprep;
  459. writel_relaxed(LPTR_DFT_TIMEOUT, qspi->io_base + QUADSPI_LPTR);
  460. writel_relaxed(CR_PRESC(presc) | CR_FTHRES(3) | CR_TCEN | CR_SSHIFT
  461. | CR_EN, qspi->io_base + QUADSPI_CR);
  462. /*
  463. * in stm32 qspi controller, QUADSPI_DCR register has a fsize field
  464. * which define the size of nor flash.
  465. * if fsize is NULL, the controller can't sent spi-nor command.
  466. * set a temporary value just to discover the nor flash with
  467. * "spi_nor_scan". After, the right value (mtd->size) can be set.
  468. */
  469. flash->fsize = FSIZE_VAL(SZ_1K);
  470. ret = spi_nor_scan(&flash->nor, NULL, &hwcaps);
  471. if (ret) {
  472. dev_err(qspi->dev, "device scan failed\n");
  473. return ret;
  474. }
  475. flash->fsize = FSIZE_VAL(mtd->size);
  476. flash->prefetch_limit = mtd->size - STM32_QSPI_FIFO_SZ;
  477. flash->read_mode = CCR_FMODE_MM;
  478. if (mtd->size > qspi->mm_size)
  479. flash->read_mode = CCR_FMODE_INDR;
  480. writel_relaxed(DCR_CSHT(1), qspi->io_base + QUADSPI_DCR);
  481. ret = mtd_device_register(mtd, NULL, 0);
  482. if (ret) {
  483. dev_err(qspi->dev, "mtd device parse failed\n");
  484. return ret;
  485. }
  486. flash->registered = true;
  487. dev_dbg(qspi->dev, "read mm:%s cs:%d bus:%d\n",
  488. flash->read_mode == CCR_FMODE_MM ? "yes" : "no", cs_num, width);
  489. return 0;
  490. }
  491. static void stm32_qspi_mtd_free(struct stm32_qspi *qspi)
  492. {
  493. int i;
  494. for (i = 0; i < STM32_MAX_NORCHIP; i++)
  495. if (qspi->flash[i].registered)
  496. mtd_device_unregister(&qspi->flash[i].nor.mtd);
  497. }
  498. static int stm32_qspi_probe(struct platform_device *pdev)
  499. {
  500. struct device *dev = &pdev->dev;
  501. struct device_node *flash_np;
  502. struct reset_control *rstc;
  503. struct stm32_qspi *qspi;
  504. struct resource *res;
  505. int ret, irq;
  506. qspi = devm_kzalloc(dev, sizeof(*qspi), GFP_KERNEL);
  507. if (!qspi)
  508. return -ENOMEM;
  509. qspi->nor_num = of_get_child_count(dev->of_node);
  510. if (!qspi->nor_num || qspi->nor_num > STM32_MAX_NORCHIP)
  511. return -ENODEV;
  512. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi");
  513. qspi->io_base = devm_ioremap_resource(dev, res);
  514. if (IS_ERR(qspi->io_base))
  515. return PTR_ERR(qspi->io_base);
  516. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mm");
  517. qspi->mm_base = devm_ioremap_resource(dev, res);
  518. if (IS_ERR(qspi->mm_base))
  519. return PTR_ERR(qspi->mm_base);
  520. qspi->mm_size = resource_size(res);
  521. irq = platform_get_irq(pdev, 0);
  522. ret = devm_request_irq(dev, irq, stm32_qspi_irq, 0,
  523. dev_name(dev), qspi);
  524. if (ret) {
  525. dev_err(dev, "failed to request irq\n");
  526. return ret;
  527. }
  528. init_completion(&qspi->cmd_completion);
  529. qspi->clk = devm_clk_get(dev, NULL);
  530. if (IS_ERR(qspi->clk))
  531. return PTR_ERR(qspi->clk);
  532. qspi->clk_rate = clk_get_rate(qspi->clk);
  533. if (!qspi->clk_rate)
  534. return -EINVAL;
  535. ret = clk_prepare_enable(qspi->clk);
  536. if (ret) {
  537. dev_err(dev, "can not enable the clock\n");
  538. return ret;
  539. }
  540. rstc = devm_reset_control_get_exclusive(dev, NULL);
  541. if (!IS_ERR(rstc)) {
  542. reset_control_assert(rstc);
  543. udelay(2);
  544. reset_control_deassert(rstc);
  545. }
  546. qspi->dev = dev;
  547. platform_set_drvdata(pdev, qspi);
  548. mutex_init(&qspi->lock);
  549. for_each_available_child_of_node(dev->of_node, flash_np) {
  550. ret = stm32_qspi_flash_setup(qspi, flash_np);
  551. if (ret) {
  552. dev_err(dev, "unable to setup flash chip\n");
  553. goto err_flash;
  554. }
  555. }
  556. return 0;
  557. err_flash:
  558. mutex_destroy(&qspi->lock);
  559. stm32_qspi_mtd_free(qspi);
  560. clk_disable_unprepare(qspi->clk);
  561. return ret;
  562. }
  563. static int stm32_qspi_remove(struct platform_device *pdev)
  564. {
  565. struct stm32_qspi *qspi = platform_get_drvdata(pdev);
  566. /* disable qspi */
  567. writel_relaxed(0, qspi->io_base + QUADSPI_CR);
  568. stm32_qspi_mtd_free(qspi);
  569. mutex_destroy(&qspi->lock);
  570. clk_disable_unprepare(qspi->clk);
  571. return 0;
  572. }
  573. static const struct of_device_id stm32_qspi_match[] = {
  574. {.compatible = "st,stm32f469-qspi"},
  575. {}
  576. };
  577. MODULE_DEVICE_TABLE(of, stm32_qspi_match);
  578. static struct platform_driver stm32_qspi_driver = {
  579. .probe = stm32_qspi_probe,
  580. .remove = stm32_qspi_remove,
  581. .driver = {
  582. .name = "stm32-quadspi",
  583. .of_match_table = stm32_qspi_match,
  584. },
  585. };
  586. module_platform_driver(stm32_qspi_driver);
  587. MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
  588. MODULE_DESCRIPTION("STMicroelectronics STM32 quad spi driver");
  589. MODULE_LICENSE("GPL v2");