atmel-quadspi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * Driver for Atmel QSPI Controller
  3. *
  4. * Copyright (C) 2015 Atmel Corporation
  5. *
  6. * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * This driver is based on drivers/mtd/spi-nor/fsl-quadspi.c from Freescale.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/clk.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/delay.h>
  27. #include <linux/err.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/mtd/spi-nor.h>
  32. #include <linux/platform_data/atmel.h>
  33. #include <linux/of.h>
  34. #include <linux/io.h>
  35. #include <linux/gpio.h>
  36. #include <linux/pinctrl/consumer.h>
  37. /* QSPI register offsets */
  38. #define QSPI_CR 0x0000 /* Control Register */
  39. #define QSPI_MR 0x0004 /* Mode Register */
  40. #define QSPI_RD 0x0008 /* Receive Data Register */
  41. #define QSPI_TD 0x000c /* Transmit Data Register */
  42. #define QSPI_SR 0x0010 /* Status Register */
  43. #define QSPI_IER 0x0014 /* Interrupt Enable Register */
  44. #define QSPI_IDR 0x0018 /* Interrupt Disable Register */
  45. #define QSPI_IMR 0x001c /* Interrupt Mask Register */
  46. #define QSPI_SCR 0x0020 /* Serial Clock Register */
  47. #define QSPI_IAR 0x0030 /* Instruction Address Register */
  48. #define QSPI_ICR 0x0034 /* Instruction Code Register */
  49. #define QSPI_IFR 0x0038 /* Instruction Frame Register */
  50. #define QSPI_SMR 0x0040 /* Scrambling Mode Register */
  51. #define QSPI_SKR 0x0044 /* Scrambling Key Register */
  52. #define QSPI_WPMR 0x00E4 /* Write Protection Mode Register */
  53. #define QSPI_WPSR 0x00E8 /* Write Protection Status Register */
  54. #define QSPI_VERSION 0x00FC /* Version Register */
  55. /* Bitfields in QSPI_CR (Control Register) */
  56. #define QSPI_CR_QSPIEN BIT(0)
  57. #define QSPI_CR_QSPIDIS BIT(1)
  58. #define QSPI_CR_SWRST BIT(7)
  59. #define QSPI_CR_LASTXFER BIT(24)
  60. /* Bitfields in QSPI_MR (Mode Register) */
  61. #define QSPI_MR_SSM BIT(0)
  62. #define QSPI_MR_LLB BIT(1)
  63. #define QSPI_MR_WDRBT BIT(2)
  64. #define QSPI_MR_SMRM BIT(3)
  65. #define QSPI_MR_CSMODE_MASK GENMASK(5, 4)
  66. #define QSPI_MR_CSMODE_NOT_RELOADED (0 << 4)
  67. #define QSPI_MR_CSMODE_LASTXFER (1 << 4)
  68. #define QSPI_MR_CSMODE_SYSTEMATICALLY (2 << 4)
  69. #define QSPI_MR_NBBITS_MASK GENMASK(11, 8)
  70. #define QSPI_MR_NBBITS(n) ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK)
  71. #define QSPI_MR_DLYBCT_MASK GENMASK(23, 16)
  72. #define QSPI_MR_DLYBCT(n) (((n) << 16) & QSPI_MR_DLYBCT_MASK)
  73. #define QSPI_MR_DLYCS_MASK GENMASK(31, 24)
  74. #define QSPI_MR_DLYCS(n) (((n) << 24) & QSPI_MR_DLYCS_MASK)
  75. /* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR */
  76. #define QSPI_SR_RDRF BIT(0)
  77. #define QSPI_SR_TDRE BIT(1)
  78. #define QSPI_SR_TXEMPTY BIT(2)
  79. #define QSPI_SR_OVRES BIT(3)
  80. #define QSPI_SR_CSR BIT(8)
  81. #define QSPI_SR_CSS BIT(9)
  82. #define QSPI_SR_INSTRE BIT(10)
  83. #define QSPI_SR_QSPIENS BIT(24)
  84. #define QSPI_SR_CMD_COMPLETED (QSPI_SR_INSTRE | QSPI_SR_CSR)
  85. /* Bitfields in QSPI_SCR (Serial Clock Register) */
  86. #define QSPI_SCR_CPOL BIT(0)
  87. #define QSPI_SCR_CPHA BIT(1)
  88. #define QSPI_SCR_SCBR_MASK GENMASK(15, 8)
  89. #define QSPI_SCR_SCBR(n) (((n) << 8) & QSPI_SCR_SCBR_MASK)
  90. #define QSPI_SCR_DLYBS_MASK GENMASK(23, 16)
  91. #define QSPI_SCR_DLYBS(n) (((n) << 16) & QSPI_SCR_DLYBS_MASK)
  92. /* Bitfields in QSPI_ICR (Instruction Code Register) */
  93. #define QSPI_ICR_INST_MASK GENMASK(7, 0)
  94. #define QSPI_ICR_INST(inst) (((inst) << 0) & QSPI_ICR_INST_MASK)
  95. #define QSPI_ICR_OPT_MASK GENMASK(23, 16)
  96. #define QSPI_ICR_OPT(opt) (((opt) << 16) & QSPI_ICR_OPT_MASK)
  97. /* Bitfields in QSPI_IFR (Instruction Frame Register) */
  98. #define QSPI_IFR_WIDTH_MASK GENMASK(2, 0)
  99. #define QSPI_IFR_WIDTH_SINGLE_BIT_SPI (0 << 0)
  100. #define QSPI_IFR_WIDTH_DUAL_OUTPUT (1 << 0)
  101. #define QSPI_IFR_WIDTH_QUAD_OUTPUT (2 << 0)
  102. #define QSPI_IFR_WIDTH_DUAL_IO (3 << 0)
  103. #define QSPI_IFR_WIDTH_QUAD_IO (4 << 0)
  104. #define QSPI_IFR_WIDTH_DUAL_CMD (5 << 0)
  105. #define QSPI_IFR_WIDTH_QUAD_CMD (6 << 0)
  106. #define QSPI_IFR_INSTEN BIT(4)
  107. #define QSPI_IFR_ADDREN BIT(5)
  108. #define QSPI_IFR_OPTEN BIT(6)
  109. #define QSPI_IFR_DATAEN BIT(7)
  110. #define QSPI_IFR_OPTL_MASK GENMASK(9, 8)
  111. #define QSPI_IFR_OPTL_1BIT (0 << 8)
  112. #define QSPI_IFR_OPTL_2BIT (1 << 8)
  113. #define QSPI_IFR_OPTL_4BIT (2 << 8)
  114. #define QSPI_IFR_OPTL_8BIT (3 << 8)
  115. #define QSPI_IFR_ADDRL BIT(10)
  116. #define QSPI_IFR_TFRTYP_MASK GENMASK(13, 12)
  117. #define QSPI_IFR_TFRTYP_TRSFR_READ (0 << 12)
  118. #define QSPI_IFR_TFRTYP_TRSFR_READ_MEM (1 << 12)
  119. #define QSPI_IFR_TFRTYP_TRSFR_WRITE (2 << 12)
  120. #define QSPI_IFR_TFRTYP_TRSFR_WRITE_MEM (3 << 13)
  121. #define QSPI_IFR_CRM BIT(14)
  122. #define QSPI_IFR_NBDUM_MASK GENMASK(20, 16)
  123. #define QSPI_IFR_NBDUM(n) (((n) << 16) & QSPI_IFR_NBDUM_MASK)
  124. /* Bitfields in QSPI_SMR (Scrambling Mode Register) */
  125. #define QSPI_SMR_SCREN BIT(0)
  126. #define QSPI_SMR_RVDIS BIT(1)
  127. /* Bitfields in QSPI_WPMR (Write Protection Mode Register) */
  128. #define QSPI_WPMR_WPEN BIT(0)
  129. #define QSPI_WPMR_WPKEY_MASK GENMASK(31, 8)
  130. #define QSPI_WPMR_WPKEY(wpkey) (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK)
  131. /* Bitfields in QSPI_WPSR (Write Protection Status Register) */
  132. #define QSPI_WPSR_WPVS BIT(0)
  133. #define QSPI_WPSR_WPVSRC_MASK GENMASK(15, 8)
  134. #define QSPI_WPSR_WPVSRC(src) (((src) << 8) & QSPI_WPSR_WPVSRC)
  135. struct atmel_qspi {
  136. void __iomem *regs;
  137. void __iomem *mem;
  138. struct clk *clk;
  139. struct platform_device *pdev;
  140. u32 pending;
  141. struct spi_nor nor;
  142. u32 clk_rate;
  143. struct completion cmd_completion;
  144. };
  145. struct atmel_qspi_command {
  146. union {
  147. struct {
  148. u32 instruction:1;
  149. u32 address:3;
  150. u32 mode:1;
  151. u32 dummy:1;
  152. u32 data:1;
  153. u32 reserved:25;
  154. } bits;
  155. u32 word;
  156. } enable;
  157. u8 instruction;
  158. u8 mode;
  159. u8 num_mode_cycles;
  160. u8 num_dummy_cycles;
  161. u32 address;
  162. size_t buf_len;
  163. const void *tx_buf;
  164. void *rx_buf;
  165. };
  166. /* Register access functions */
  167. static inline u32 qspi_readl(struct atmel_qspi *aq, u32 reg)
  168. {
  169. return readl_relaxed(aq->regs + reg);
  170. }
  171. static inline void qspi_writel(struct atmel_qspi *aq, u32 reg, u32 value)
  172. {
  173. writel_relaxed(value, aq->regs + reg);
  174. }
  175. static int atmel_qspi_run_transfer(struct atmel_qspi *aq,
  176. const struct atmel_qspi_command *cmd)
  177. {
  178. void __iomem *ahb_mem;
  179. /* Then fallback to a PIO transfer (memcpy() DOES NOT work!) */
  180. ahb_mem = aq->mem;
  181. if (cmd->enable.bits.address)
  182. ahb_mem += cmd->address;
  183. if (cmd->tx_buf)
  184. _memcpy_toio(ahb_mem, cmd->tx_buf, cmd->buf_len);
  185. else
  186. _memcpy_fromio(cmd->rx_buf, ahb_mem, cmd->buf_len);
  187. return 0;
  188. }
  189. #ifdef DEBUG
  190. static void atmel_qspi_debug_command(struct atmel_qspi *aq,
  191. const struct atmel_qspi_command *cmd,
  192. u32 ifr)
  193. {
  194. u8 cmd_buf[SPI_NOR_MAX_CMD_SIZE];
  195. size_t len = 0;
  196. int i;
  197. if (cmd->enable.bits.instruction)
  198. cmd_buf[len++] = cmd->instruction;
  199. for (i = cmd->enable.bits.address-1; i >= 0; --i)
  200. cmd_buf[len++] = (cmd->address >> (i << 3)) & 0xff;
  201. if (cmd->enable.bits.mode)
  202. cmd_buf[len++] = cmd->mode;
  203. if (cmd->enable.bits.dummy) {
  204. int num = cmd->num_dummy_cycles;
  205. switch (ifr & QSPI_IFR_WIDTH_MASK) {
  206. case QSPI_IFR_WIDTH_SINGLE_BIT_SPI:
  207. case QSPI_IFR_WIDTH_DUAL_OUTPUT:
  208. case QSPI_IFR_WIDTH_QUAD_OUTPUT:
  209. num >>= 3;
  210. break;
  211. case QSPI_IFR_WIDTH_DUAL_IO:
  212. case QSPI_IFR_WIDTH_DUAL_CMD:
  213. num >>= 2;
  214. break;
  215. case QSPI_IFR_WIDTH_QUAD_IO:
  216. case QSPI_IFR_WIDTH_QUAD_CMD:
  217. num >>= 1;
  218. break;
  219. default:
  220. return;
  221. }
  222. for (i = 0; i < num; ++i)
  223. cmd_buf[len++] = 0;
  224. }
  225. /* Dump the SPI command */
  226. print_hex_dump(KERN_DEBUG, "qspi cmd: ", DUMP_PREFIX_NONE,
  227. 32, 1, cmd_buf, len, false);
  228. #ifdef VERBOSE_DEBUG
  229. /* If verbose debug is enabled, also dump the TX data */
  230. if (cmd->enable.bits.data && cmd->tx_buf)
  231. print_hex_dump(KERN_DEBUG, "qspi tx : ", DUMP_PREFIX_NONE,
  232. 32, 1, cmd->tx_buf, cmd->buf_len, false);
  233. #endif
  234. }
  235. #else
  236. #define atmel_qspi_debug_command(aq, cmd, ifr)
  237. #endif
  238. static int atmel_qspi_run_command(struct atmel_qspi *aq,
  239. const struct atmel_qspi_command *cmd,
  240. u32 ifr_tfrtyp, u32 ifr_width)
  241. {
  242. u32 iar, icr, ifr, sr;
  243. int err = 0;
  244. iar = 0;
  245. icr = 0;
  246. ifr = ifr_tfrtyp | ifr_width;
  247. /* Compute instruction parameters */
  248. if (cmd->enable.bits.instruction) {
  249. icr |= QSPI_ICR_INST(cmd->instruction);
  250. ifr |= QSPI_IFR_INSTEN;
  251. }
  252. /* Compute address parameters */
  253. switch (cmd->enable.bits.address) {
  254. case 4:
  255. ifr |= QSPI_IFR_ADDRL;
  256. /* fall through to the 24bit (3 byte) address case. */
  257. case 3:
  258. iar = (cmd->enable.bits.data) ? 0 : cmd->address;
  259. ifr |= QSPI_IFR_ADDREN;
  260. break;
  261. case 0:
  262. break;
  263. default:
  264. return -EINVAL;
  265. }
  266. /* Compute option parameters */
  267. if (cmd->enable.bits.mode && cmd->num_mode_cycles) {
  268. u32 mode_cycle_bits, mode_bits;
  269. icr |= QSPI_ICR_OPT(cmd->mode);
  270. ifr |= QSPI_IFR_OPTEN;
  271. switch (ifr & QSPI_IFR_WIDTH_MASK) {
  272. case QSPI_IFR_WIDTH_SINGLE_BIT_SPI:
  273. case QSPI_IFR_WIDTH_DUAL_OUTPUT:
  274. case QSPI_IFR_WIDTH_QUAD_OUTPUT:
  275. mode_cycle_bits = 1;
  276. break;
  277. case QSPI_IFR_WIDTH_DUAL_IO:
  278. case QSPI_IFR_WIDTH_DUAL_CMD:
  279. mode_cycle_bits = 2;
  280. break;
  281. case QSPI_IFR_WIDTH_QUAD_IO:
  282. case QSPI_IFR_WIDTH_QUAD_CMD:
  283. mode_cycle_bits = 4;
  284. break;
  285. default:
  286. return -EINVAL;
  287. }
  288. mode_bits = cmd->num_mode_cycles * mode_cycle_bits;
  289. switch (mode_bits) {
  290. case 1:
  291. ifr |= QSPI_IFR_OPTL_1BIT;
  292. break;
  293. case 2:
  294. ifr |= QSPI_IFR_OPTL_2BIT;
  295. break;
  296. case 4:
  297. ifr |= QSPI_IFR_OPTL_4BIT;
  298. break;
  299. case 8:
  300. ifr |= QSPI_IFR_OPTL_8BIT;
  301. break;
  302. default:
  303. return -EINVAL;
  304. }
  305. }
  306. /* Set number of dummy cycles */
  307. if (cmd->enable.bits.dummy)
  308. ifr |= QSPI_IFR_NBDUM(cmd->num_dummy_cycles);
  309. /* Set data enable */
  310. if (cmd->enable.bits.data) {
  311. ifr |= QSPI_IFR_DATAEN;
  312. /* Special case for Continuous Read Mode */
  313. if (!cmd->tx_buf && !cmd->rx_buf)
  314. ifr |= QSPI_IFR_CRM;
  315. }
  316. /* Clear pending interrupts */
  317. (void)qspi_readl(aq, QSPI_SR);
  318. /* Set QSPI Instruction Frame registers */
  319. atmel_qspi_debug_command(aq, cmd, ifr);
  320. qspi_writel(aq, QSPI_IAR, iar);
  321. qspi_writel(aq, QSPI_ICR, icr);
  322. qspi_writel(aq, QSPI_IFR, ifr);
  323. /* Skip to the final steps if there is no data */
  324. if (!cmd->enable.bits.data)
  325. goto no_data;
  326. /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */
  327. (void)qspi_readl(aq, QSPI_IFR);
  328. /* Stop here for continuous read */
  329. if (!cmd->tx_buf && !cmd->rx_buf)
  330. return 0;
  331. /* Send/Receive data */
  332. err = atmel_qspi_run_transfer(aq, cmd);
  333. /* Release the chip-select */
  334. qspi_writel(aq, QSPI_CR, QSPI_CR_LASTXFER);
  335. if (err)
  336. return err;
  337. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  338. /*
  339. * If verbose debug is enabled, also dump the RX data in addition to
  340. * the SPI command previously dumped by atmel_qspi_debug_command()
  341. */
  342. if (cmd->rx_buf)
  343. print_hex_dump(KERN_DEBUG, "qspi rx : ", DUMP_PREFIX_NONE,
  344. 32, 1, cmd->rx_buf, cmd->buf_len, false);
  345. #endif
  346. no_data:
  347. /* Poll INSTRuction End status */
  348. sr = qspi_readl(aq, QSPI_SR);
  349. if ((sr & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED)
  350. return err;
  351. /* Wait for INSTRuction End interrupt */
  352. reinit_completion(&aq->cmd_completion);
  353. aq->pending = sr & QSPI_SR_CMD_COMPLETED;
  354. qspi_writel(aq, QSPI_IER, QSPI_SR_CMD_COMPLETED);
  355. if (!wait_for_completion_timeout(&aq->cmd_completion,
  356. msecs_to_jiffies(1000)))
  357. err = -ETIMEDOUT;
  358. qspi_writel(aq, QSPI_IDR, QSPI_SR_CMD_COMPLETED);
  359. return err;
  360. }
  361. static int atmel_qspi_read_reg(struct spi_nor *nor, u8 opcode,
  362. u8 *buf, int len)
  363. {
  364. struct atmel_qspi *aq = nor->priv;
  365. struct atmel_qspi_command cmd;
  366. memset(&cmd, 0, sizeof(cmd));
  367. cmd.enable.bits.instruction = 1;
  368. cmd.enable.bits.data = 1;
  369. cmd.instruction = opcode;
  370. cmd.rx_buf = buf;
  371. cmd.buf_len = len;
  372. return atmel_qspi_run_command(aq, &cmd, QSPI_IFR_TFRTYP_TRSFR_READ,
  373. QSPI_IFR_WIDTH_SINGLE_BIT_SPI);
  374. }
  375. static int atmel_qspi_write_reg(struct spi_nor *nor, u8 opcode,
  376. u8 *buf, int len)
  377. {
  378. struct atmel_qspi *aq = nor->priv;
  379. struct atmel_qspi_command cmd;
  380. memset(&cmd, 0, sizeof(cmd));
  381. cmd.enable.bits.instruction = 1;
  382. cmd.enable.bits.data = (buf != NULL && len > 0);
  383. cmd.instruction = opcode;
  384. cmd.tx_buf = buf;
  385. cmd.buf_len = len;
  386. return atmel_qspi_run_command(aq, &cmd, QSPI_IFR_TFRTYP_TRSFR_WRITE,
  387. QSPI_IFR_WIDTH_SINGLE_BIT_SPI);
  388. }
  389. static ssize_t atmel_qspi_write(struct spi_nor *nor, loff_t to, size_t len,
  390. const u_char *write_buf)
  391. {
  392. struct atmel_qspi *aq = nor->priv;
  393. struct atmel_qspi_command cmd;
  394. ssize_t ret;
  395. memset(&cmd, 0, sizeof(cmd));
  396. cmd.enable.bits.instruction = 1;
  397. cmd.enable.bits.address = nor->addr_width;
  398. cmd.enable.bits.data = 1;
  399. cmd.instruction = nor->program_opcode;
  400. cmd.address = (u32)to;
  401. cmd.tx_buf = write_buf;
  402. cmd.buf_len = len;
  403. ret = atmel_qspi_run_command(aq, &cmd, QSPI_IFR_TFRTYP_TRSFR_WRITE_MEM,
  404. QSPI_IFR_WIDTH_SINGLE_BIT_SPI);
  405. return (ret < 0) ? ret : len;
  406. }
  407. static int atmel_qspi_erase(struct spi_nor *nor, loff_t offs)
  408. {
  409. struct atmel_qspi *aq = nor->priv;
  410. struct atmel_qspi_command cmd;
  411. memset(&cmd, 0, sizeof(cmd));
  412. cmd.enable.bits.instruction = 1;
  413. cmd.enable.bits.address = nor->addr_width;
  414. cmd.instruction = nor->erase_opcode;
  415. cmd.address = (u32)offs;
  416. return atmel_qspi_run_command(aq, &cmd, QSPI_IFR_TFRTYP_TRSFR_WRITE,
  417. QSPI_IFR_WIDTH_SINGLE_BIT_SPI);
  418. }
  419. static ssize_t atmel_qspi_read(struct spi_nor *nor, loff_t from, size_t len,
  420. u_char *read_buf)
  421. {
  422. struct atmel_qspi *aq = nor->priv;
  423. struct atmel_qspi_command cmd;
  424. u8 num_mode_cycles, num_dummy_cycles;
  425. u32 ifr_width;
  426. ssize_t ret;
  427. switch (nor->flash_read) {
  428. case SPI_NOR_NORMAL:
  429. case SPI_NOR_FAST:
  430. ifr_width = QSPI_IFR_WIDTH_SINGLE_BIT_SPI;
  431. break;
  432. case SPI_NOR_DUAL:
  433. ifr_width = QSPI_IFR_WIDTH_DUAL_OUTPUT;
  434. break;
  435. case SPI_NOR_QUAD:
  436. ifr_width = QSPI_IFR_WIDTH_QUAD_OUTPUT;
  437. break;
  438. default:
  439. return -EINVAL;
  440. }
  441. if (nor->read_dummy >= 2) {
  442. num_mode_cycles = 2;
  443. num_dummy_cycles = nor->read_dummy - 2;
  444. } else {
  445. num_mode_cycles = nor->read_dummy;
  446. num_dummy_cycles = 0;
  447. }
  448. memset(&cmd, 0, sizeof(cmd));
  449. cmd.enable.bits.instruction = 1;
  450. cmd.enable.bits.address = nor->addr_width;
  451. cmd.enable.bits.mode = (num_mode_cycles > 0);
  452. cmd.enable.bits.dummy = (num_dummy_cycles > 0);
  453. cmd.enable.bits.data = 1;
  454. cmd.instruction = nor->read_opcode;
  455. cmd.address = (u32)from;
  456. cmd.mode = 0xff; /* This value prevents from entering the 0-4-4 mode */
  457. cmd.num_mode_cycles = num_mode_cycles;
  458. cmd.num_dummy_cycles = num_dummy_cycles;
  459. cmd.rx_buf = read_buf;
  460. cmd.buf_len = len;
  461. ret = atmel_qspi_run_command(aq, &cmd, QSPI_IFR_TFRTYP_TRSFR_READ_MEM,
  462. ifr_width);
  463. return (ret < 0) ? ret : len;
  464. }
  465. static int atmel_qspi_init(struct atmel_qspi *aq)
  466. {
  467. unsigned long src_rate;
  468. u32 mr, scr, scbr;
  469. /* Reset the QSPI controller */
  470. qspi_writel(aq, QSPI_CR, QSPI_CR_SWRST);
  471. /* Set the QSPI controller in Serial Memory Mode */
  472. mr = QSPI_MR_NBBITS(8) | QSPI_MR_SSM;
  473. qspi_writel(aq, QSPI_MR, mr);
  474. src_rate = clk_get_rate(aq->clk);
  475. if (!src_rate)
  476. return -EINVAL;
  477. /* Compute the QSPI baudrate */
  478. scbr = DIV_ROUND_UP(src_rate, aq->clk_rate);
  479. if (scbr > 0)
  480. scbr--;
  481. scr = QSPI_SCR_SCBR(scbr);
  482. qspi_writel(aq, QSPI_SCR, scr);
  483. /* Enable the QSPI controller */
  484. qspi_writel(aq, QSPI_CR, QSPI_CR_QSPIEN);
  485. return 0;
  486. }
  487. static irqreturn_t atmel_qspi_interrupt(int irq, void *dev_id)
  488. {
  489. struct atmel_qspi *aq = (struct atmel_qspi *)dev_id;
  490. u32 status, mask, pending;
  491. status = qspi_readl(aq, QSPI_SR);
  492. mask = qspi_readl(aq, QSPI_IMR);
  493. pending = status & mask;
  494. if (!pending)
  495. return IRQ_NONE;
  496. aq->pending |= pending;
  497. if ((aq->pending & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED)
  498. complete(&aq->cmd_completion);
  499. return IRQ_HANDLED;
  500. }
  501. static int atmel_qspi_probe(struct platform_device *pdev)
  502. {
  503. struct device_node *child, *np = pdev->dev.of_node;
  504. struct atmel_qspi *aq;
  505. struct resource *res;
  506. struct spi_nor *nor;
  507. struct mtd_info *mtd;
  508. int irq, err = 0;
  509. if (of_get_child_count(np) != 1)
  510. return -ENODEV;
  511. child = of_get_next_child(np, NULL);
  512. aq = devm_kzalloc(&pdev->dev, sizeof(*aq), GFP_KERNEL);
  513. if (!aq) {
  514. err = -ENOMEM;
  515. goto exit;
  516. }
  517. platform_set_drvdata(pdev, aq);
  518. init_completion(&aq->cmd_completion);
  519. aq->pdev = pdev;
  520. /* Map the registers */
  521. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_base");
  522. aq->regs = devm_ioremap_resource(&pdev->dev, res);
  523. if (IS_ERR(aq->regs)) {
  524. dev_err(&pdev->dev, "missing registers\n");
  525. err = PTR_ERR(aq->regs);
  526. goto exit;
  527. }
  528. /* Map the AHB memory */
  529. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mmap");
  530. aq->mem = devm_ioremap_resource(&pdev->dev, res);
  531. if (IS_ERR(aq->mem)) {
  532. dev_err(&pdev->dev, "missing AHB memory\n");
  533. err = PTR_ERR(aq->mem);
  534. goto exit;
  535. }
  536. /* Get the peripheral clock */
  537. aq->clk = devm_clk_get(&pdev->dev, NULL);
  538. if (IS_ERR(aq->clk)) {
  539. dev_err(&pdev->dev, "missing peripheral clock\n");
  540. err = PTR_ERR(aq->clk);
  541. goto exit;
  542. }
  543. /* Enable the peripheral clock */
  544. err = clk_prepare_enable(aq->clk);
  545. if (err) {
  546. dev_err(&pdev->dev, "failed to enable the peripheral clock\n");
  547. goto exit;
  548. }
  549. /* Request the IRQ */
  550. irq = platform_get_irq(pdev, 0);
  551. if (irq < 0) {
  552. dev_err(&pdev->dev, "missing IRQ\n");
  553. err = irq;
  554. goto disable_clk;
  555. }
  556. err = devm_request_irq(&pdev->dev, irq, atmel_qspi_interrupt,
  557. 0, dev_name(&pdev->dev), aq);
  558. if (err)
  559. goto disable_clk;
  560. /* Setup the spi-nor */
  561. nor = &aq->nor;
  562. mtd = &nor->mtd;
  563. nor->dev = &pdev->dev;
  564. spi_nor_set_flash_node(nor, child);
  565. nor->priv = aq;
  566. mtd->priv = nor;
  567. nor->read_reg = atmel_qspi_read_reg;
  568. nor->write_reg = atmel_qspi_write_reg;
  569. nor->read = atmel_qspi_read;
  570. nor->write = atmel_qspi_write;
  571. nor->erase = atmel_qspi_erase;
  572. err = of_property_read_u32(child, "spi-max-frequency", &aq->clk_rate);
  573. if (err < 0)
  574. goto disable_clk;
  575. err = atmel_qspi_init(aq);
  576. if (err)
  577. goto disable_clk;
  578. err = spi_nor_scan(nor, NULL, SPI_NOR_QUAD);
  579. if (err)
  580. goto disable_clk;
  581. err = mtd_device_register(mtd, NULL, 0);
  582. if (err)
  583. goto disable_clk;
  584. of_node_put(child);
  585. return 0;
  586. disable_clk:
  587. clk_disable_unprepare(aq->clk);
  588. exit:
  589. of_node_put(child);
  590. return err;
  591. }
  592. static int atmel_qspi_remove(struct platform_device *pdev)
  593. {
  594. struct atmel_qspi *aq = platform_get_drvdata(pdev);
  595. mtd_device_unregister(&aq->nor.mtd);
  596. qspi_writel(aq, QSPI_CR, QSPI_CR_QSPIDIS);
  597. clk_disable_unprepare(aq->clk);
  598. return 0;
  599. }
  600. static const struct of_device_id atmel_qspi_dt_ids[] = {
  601. { .compatible = "atmel,sama5d2-qspi" },
  602. { /* sentinel */ }
  603. };
  604. MODULE_DEVICE_TABLE(of, atmel_qspi_dt_ids);
  605. static struct platform_driver atmel_qspi_driver = {
  606. .driver = {
  607. .name = "atmel_qspi",
  608. .of_match_table = atmel_qspi_dt_ids,
  609. },
  610. .probe = atmel_qspi_probe,
  611. .remove = atmel_qspi_remove,
  612. };
  613. module_platform_driver(atmel_qspi_driver);
  614. MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
  615. MODULE_DESCRIPTION("Atmel QSPI Controller driver");
  616. MODULE_LICENSE("GPL v2");