atmel-quadspi.c 21 KB

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