phytium-quadspi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /*
  2. * Phytium SPI core controller driver.
  3. *
  4. * Copyright (c) 2019, Phytium Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/errno.h>
  17. #include <linux/io.h>
  18. #include <linux/iopoll.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <linux/mtd/spi-nor.h>
  24. #include <linux/mutex.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/reset.h>
  29. #include <linux/sizes.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/swab.h>
  32. #define QSPI_FLASH_CAP_REG 0x000
  33. #define QSPI_RD_CFG_REG 0x004
  34. #define QSPI_WR_CFG_REG 0x008
  35. #define QSPI_FLUSH_REG 0x00C
  36. #define QSPI_CMD_PORT_REG 0x010
  37. #define QSPI_ADDR_PORT_REG 0x014
  38. #define QSPI_HD_PORT_REG 0x018
  39. #define QSPI_LD_PORT_REG 0x01C
  40. #define QSPI_FUN_SET_REG 0x020
  41. #define QSPI_WIP_REG 0x024
  42. #define QSPI_WP_REG 0x028
  43. #define QSPI_MODE_REG 0x02C
  44. #define QSPI_FLASH_CAP_NUM_SHIFT 3
  45. #define QSPI_FLASH_CAP_NUM_MASK (0x3 << QSPI_FLASH_CAP_NUM_SHIFT)
  46. #define QSPI_FLASH_CAP_CAP_SHIFT 0
  47. #define QSPI_FLASH_CAP_CAP_MASK (0x7 << QSPI_FLASH_CAP_CAP_SHIFT)
  48. #define QSPI_RD_CFG_RD_CMD_SHIFT 24
  49. #define QSPI_RD_CFG_RD_CMD_MASK (0xFF << QSPI_RD_CFG_RD_CMD_SHIFT)
  50. #define QSPI_RD_CFG_RD_THROUGH_SHIFT 23
  51. #define QSPI_RD_CFG_RD_THROUGH_MASK (0x01 << QSPI_RD_CFG_RD_THROUGH_SHIFT)
  52. #define QSPI_RD_CFG_RD_TRANSFER_SHIFT 20
  53. #define QSPI_RD_CFG_RD_TRANSFER_MASK (0x07 << QSPI_RD_CFG_RD_TRANSFER_SHIFT)
  54. #define QSPI_RD_CFG_RD_ADDR_SEL_SHIFT 19
  55. #define QSPI_RD_CFG_RD_ADDR_SEL_MASK (0x1 << QSPI_RD_CFG_RD_ADDR_SEL_SHIFT)
  56. #define QSPI_RD_CFG_RD_LATENCY_SHIFT 18
  57. #define QSPI_RD_CFG_RD_LATENCY_MASK (0x1 << QSPI_RD_CFG_RD_LATENCY_SHIFT)
  58. #define QSPI_RD_CFG_MODE_BYTE_SHIFT 17
  59. #define QSPI_RD_CFG_MODE_BYTE_MASK (0x1 << QSPI_RD_CFG_MODE_BYTE_SHIFT)
  60. #define QSPI_RD_CFG_CMD_SIGN_SHIFT 9
  61. #define QSPI_RD_CFG_CMD_SIGN_MASK (0xFF << QSPI_RD_CFG_CMD_SIGN_SHIFT)
  62. #define QSPI_RD_CFG_DUMMY_SHIFT 4
  63. #define QSPI_RD_CFG_DUMMY_MASK (0x1F << QSPI_RD_CFG_DUMMY_SHIFT)
  64. #define QSPI_RD_CFG_D_BUFFER_SHIFT 3
  65. #define QSPI_RD_CFG_D_BUFFER_MASK (0x1 << QSPI_RD_CFG_D_BUFFER_SHIFT)
  66. #define QSPI_RD_CFG_RD_SCK_SEL_SHIFT 0
  67. #define QSPI_RD_CFG_RD_SCK_SEL_MASK (0x3 << QSPI_RD_CFG_RD_SCK_SEL_SHIFT)
  68. #define QSPI_WR_CFG_WR_CMD_SHIFT 24
  69. #define QSPI_WR_CFG_WR_CMD_MASK (0xFF << QSPI_WR_CFG_WR_CMD_SHIFT)
  70. #define QSPI_WR_CFG_WR_WAIT_SHIFT 9
  71. #define QSPI_WR_CFG_WR_WAIT_MASK (0x01 << QSPI_WR_CFG_WR_WAIT_SHIFT)
  72. #define QSPI_WR_CFG_WR_THROUGH_SHIFT 8
  73. #define QSPI_WR_CFG_WR_THROUGH_MAS (0x01 << QSPI_WR_CFG_WR_THROUGH_SHIFT)
  74. #define QSPI_WR_CFG_WR_TRANSFER_SHIFT 5
  75. #define QSPI_WR_CFG_WR_TRANSFER_MASK (0X7 << QSPI_WR_CFG_WR_TRANSFER_SHIFT)
  76. #define QSPI_WR_CFG_WR_ADDR_SEL_SHIFT 4
  77. #define QSPI_WR_CFG_WR_ADDR_SEL_MASK (0x1 << QSPI_WR_CFG_WR_ADDR_SEL_SHIFT)
  78. #define QSPI_WR_CFG_WR_MODE_SHIFT 3
  79. #define QSPI_WR_CFG_WR_MODE (0x01 << QSPI_WR_CFG_WR_MODE_SHIFT)
  80. #define QSPI_WR_CFG_WR_SCK_SEL_SHIFT 0
  81. #define QSPI_WR_CFG_WR_SCK_SEL_MASK (0x7 << QSPI_WR_CFG_WR_SCK_SEL_SHIFT)
  82. #define QSPI_FLUSH_EN (0x1 << 0)
  83. #define QSPI_CMD_PORT_CMD_SHIFT 24
  84. #define QSPI_CMD_PORT_CMD_MASK (0xFF << QSPI_CMD_PORT_CMD_SHIFT)
  85. #define QSPI_CMD_PORT_WAIT_SHIFT 22
  86. #define QSPI_CMD_PORT_WAIT_MASK (0x1 << QSPI_CMD_PORT_WAIT_SHIFT)
  87. #define QSPI_CMD_PORT_THROUGH_SHIFT 21
  88. #define QSPI_CMD_PORT_THROUGH_MASK (0x1 << QSPI_CMD_PORT_THROUGH_SHIFT)
  89. #define QSPI_CMD_PORT_CS_SHIFT 19
  90. #define QSPI_CMD_PORT_CS_MASK (0x3 << QSPI_CMD_PORT_CS_SHIFT)
  91. #define QSPI_CMD_PORT_TRANSFER_SHIFT 16
  92. #define QSPI_CMD_PORT_TRANSFER_MASK (0x7 << QSPI_CMD_PORT_TRANSFER_SHIFT)
  93. #define QSPI_CMD_PORT_CMD_ADDR_SHIFT 15
  94. #define QSPI_CMD_PORT_CMD_ADDR_MASK (0x1 << QSPI_CMD_PORT_CMD_ADDR_SHIFT)
  95. #define QSPI_CMD_PORT_LATENCY_SHIFT 14
  96. #define QSPI_CMD_PORT_LATENCY_MASK (0x1 << QSPI_CMD_PORT_LATENCY_SHIFT)
  97. #define QSPI_CMD_PORT_DATA_TRANSFER_SHIFT 13
  98. #define QSPI_CMD_PORT_DATA_TRANSFER_MASK (0x1 << 13)
  99. #define QSPI_CMD_PORT_SEL_SHIFT 12
  100. #define QSPI_CMD_PORT_SEL_MASK (0x1 << QSPI_CMD_PORT_SEL_SHIFT)
  101. #define QSPI_CMD_PORT_DUMMY_SHIFT 7
  102. #define QSPI_CMD_PORT_DUMMY_MASK (0x1F << QSPI_CMD_PORT_DUMMY_SHIFT)
  103. #define QSPI_CMD_PORT_P_BUFFER_SHIFT 6
  104. #define QSPI_CMD_PORT_P_BUFFER_MASK (0x1 << QSPI_CMD_PORT_P_BUFFER_SHIFT)
  105. #define QSPI_CMD_PORT_RW_NUM_SHIFT 3
  106. #define QSPI_CMD_PORT_RW_NUM_MASK (0x7 << QSPI_CMD_PORT_RW_NUM_SHIFT)
  107. #define QSPI_CMD_PORT_SCK_SEL_SHIFT 0
  108. #define QSPI_CMD_PORT_SCK_SEL_MASK (0x7 << QSPI_CMD_PORT_SCK_SEL_SHIFT)
  109. #define QSPI_FUN_SET_HOLD_SHIFT 24
  110. #define QSPI_FUN_SET_HOLD_MASK (0xFF << QSPI_FUN_SET_HOLD_SHIFT)
  111. #define QSPI_FUN_SET_SETUP_SHIFT 16
  112. #define QSPI_FUN_SET_SETUP_MASK (0xFF << QSPI_FUN_SET_SETUP_SHIFT)
  113. #define QSPI_FUN_SET_DELAY_SHIFT 0
  114. #define QSPI_FUN_SET_DELAY_MASK (0xFFFF << QSPI_FUN_SET_DELAY_SHIFT)
  115. #define QSPI_WIP_W_CMD_SHIFT 24
  116. #define QSPI_WIP_W_CMD_MASK (0xFF << QSPI_WIP_W_CMD_SHIFT)
  117. #define QSPI_WIP_W_TRANSFER_SHIFT 3
  118. #define QSPI_WIP_W_TRANSFER_MASK (0x3 << QSPI_WIP_W_TRANSFER_SHIFT)
  119. #define QSPI_WIP_W_SCK_SEL_SHIFT 0
  120. #define QSPI_WIP_W_SCK_SEL_MASK (0x7 << QSPI_WIP_W_SCK_SEL_SHIFT)
  121. #define QSPI_WP_EN_SHIFT 17
  122. #define QSPI_WP_EN_MASK (0x1 << QSPI_WP_EN_SHIFT)
  123. #define QSPI_WP_IO2_SHIFT 16
  124. #define QSPI_WP_IO2_MASK (0x1 << QSPI_WP_IO2_SHIFT)
  125. #define QSPI_WP_HOLD_SHIFT 8
  126. #define QSPI_WP_HOLD_MASK (0xFF << QSPI_WP_HOLD_SHIFT)
  127. #define QSPI_WP_SETUP_SHIFT 0
  128. #define QSPI_WP_SETUP_MASK (0xFF << QSPI_WP_SETUP_SHIFT)
  129. #define QSPI_MODE_VALID_SHIFT 8
  130. #define QSPI_MODE_VALID_MASK (0xFF << QSPI_MODE_VALID_SHIFT)
  131. #define QSPI_MODE_SHIFT 0
  132. #define QSPI_MODE_MASK (0xFF << QSPI_MODE_SHIFT)
  133. #define FSIZE_VAL(size) (__fls(size) - 1)
  134. #define PHYTIUM_MAX_MMAP_S SZ_512M
  135. #define PHYTIUM_MAX_NORCHIP 2
  136. #define PHYTIUM_QSPI_FIFO_SZ 32
  137. #define PHYTIUM_QSPI_FIFO_TIMEOUT_US 50000
  138. #define PHYTIUM_QSPI_BUSY_TIMEOUT_US 100000
  139. #define PHYTIUM_SCK_SEL 0x05
  140. #define PHYTIUM_CMD_SCK_SEL 0x07
  141. #define PHYTIUM_FMODE_MM 0x01
  142. #define PHYTIUM_FMODE_IN 0x02
  143. /*
  144. * the codes of the different commands
  145. */
  146. #define CMD_WRDI 0x04
  147. #define CMD_RDID 0x9F
  148. #define CMD_RDSR 0x05
  149. #define CMD_WREN 0x06
  150. #define CMD_RDAR 0x65
  151. #define CMD_P4E 0x20
  152. #define CMD_4P4E 0x21
  153. #define CMD_BE 0x60
  154. #define CMD_4BE 0xC7
  155. #define CMD_READ 0x03
  156. #define CMD_FAST_READ 0x0B
  157. #define CMD_QOR 0x6B
  158. #define CMD_QIOR 0xEB
  159. #define CMD_DDRFR 0x0D
  160. #define CMD_DDRQIOQ 0xED
  161. #define CMD_PP 0x02
  162. #define CMD_QPP 0x32
  163. #define CMD_SE 0xD8
  164. #define CMD_4FAST_READ 0x0C
  165. #define CMD_4READ 0x13
  166. #define CMD_4QOR 0x6C
  167. #define CMD_4QIOR 0xEC
  168. #define CMD_4DDRFR 0x0E
  169. #define CMD_4DDRQIOR 0xEE
  170. #define CMD_4PP 0x12
  171. #define CMD_4QPP 0x34
  172. #define CMD_4SE 0xDC
  173. #define PHYTIUM_QSPI_1_1_1 0
  174. #define PHYTIUM_QSPI_1_1_2 1
  175. #define PHYTIUM_QSPI_1_1_4 2
  176. #define PHYTIUM_QSPI_1_2_2 3
  177. #define PHYTIUM_QSPI_1_4_4 4
  178. #define PHYTIUM_QSPI_2_2_2 5
  179. #define PHYTIUM_QSPI_4_4_4 6
  180. struct phytium_qspi_flash {
  181. struct spi_nor nor;
  182. struct phytium_qspi *qspi;
  183. u32 cs;
  184. u32 fsize;
  185. u32 presc;
  186. u32 clk_div;
  187. u32 read_mode;
  188. bool registered;
  189. u32 prefetch_limit;
  190. u32 addr_width;
  191. u32 read_cmd;
  192. };
  193. struct phytium_qspi {
  194. struct device *dev;
  195. void __iomem *io_base;
  196. void __iomem *mm_base;
  197. resource_size_t mm_size;
  198. u32 nor_num;
  199. struct clk *clk;
  200. u32 clk_rate;
  201. struct phytium_qspi_flash flash[PHYTIUM_MAX_NORCHIP];
  202. spinlock_t spinlock;
  203. /*
  204. * to protect device configuration, could be different between
  205. * 2 flash access (bk1, bk2)
  206. */
  207. struct mutex lock;
  208. };
  209. /* Need to enable p_buffer */
  210. static int memcpy_from_ftreg(struct phytium_qspi *qspi, u_char *buf, size_t len)
  211. {
  212. int i;
  213. u32 val = 0;
  214. if (!qspi || !buf)
  215. return -EINVAL;
  216. for (i = 0; i < len; i++) {
  217. if (0 == i % 4)
  218. val = readl_relaxed(qspi->io_base + QSPI_LD_PORT_REG);
  219. buf[i] = (u_char) (val >> (i % 4) * 8) & 0xFF;
  220. }
  221. return 0;
  222. }
  223. /* Not to enable p_buffer */
  224. static int memcpy_to_ftreg(struct phytium_qspi *qspi, u_char *buf, size_t len)
  225. {
  226. u32 val = 0;
  227. if (!qspi || !buf || (len >= 8))
  228. return -EINVAL;
  229. if (1 == len) {
  230. val = buf[0];
  231. } else if (2 == len) {
  232. val = buf[1];
  233. val = (val << 8) + buf[0];
  234. } else if (3 == len) {
  235. val = buf[2];
  236. val = (val << 8) + buf[1];
  237. val = (val << 8) + buf[0];
  238. } else if (4 == len) {
  239. val = buf[3];
  240. val = (val << 8) + buf[2];
  241. val = (val << 8) + buf[1];
  242. val = (val << 8) + buf[0];
  243. }
  244. writel_relaxed(val, qspi->io_base + QSPI_LD_PORT_REG);
  245. return 0;
  246. }
  247. static int phytium_qspi_wait_cmd(struct phytium_qspi *qspi)
  248. {
  249. u32 cmd = 0;
  250. u32 sr = 0;
  251. int err = 0;
  252. cmd |= CMD_RDSR << QSPI_CMD_PORT_CMD_SHIFT;
  253. cmd |= BIT(QSPI_CMD_PORT_DATA_TRANSFER_SHIFT);
  254. writel_relaxed(cmd, qspi->io_base + QSPI_CMD_PORT_REG);
  255. err = readl_relaxed_poll_timeout(qspi->io_base + QSPI_LD_PORT_REG,
  256. sr, ~(sr & 0x01), 10,
  257. PHYTIUM_QSPI_BUSY_TIMEOUT_US);
  258. if (err)
  259. dev_err(qspi->dev,
  260. "wait command process timeout (stat:%#x, sr:%#x)\n",
  261. err, sr);
  262. return err;
  263. }
  264. static int phytium_qspi_cmd_enable(struct phytium_qspi *qspi)
  265. {
  266. u32 val = 0;
  267. writel_relaxed(val, qspi->io_base + QSPI_LD_PORT_REG);
  268. return 0;
  269. }
  270. static int phytium_qspi_write_enable(struct phytium_qspi *qspi)
  271. {
  272. u32 cmd = 0;
  273. cmd = CMD_WREN << QSPI_CMD_PORT_CMD_SHIFT;
  274. cmd |= BIT(QSPI_CMD_PORT_WAIT_SHIFT);
  275. cmd |= PHYTIUM_SCK_SEL << QSPI_CMD_PORT_SCK_SEL_SHIFT;
  276. writel_relaxed(cmd, qspi->io_base + QSPI_CMD_PORT_REG);
  277. phytium_qspi_cmd_enable(qspi);
  278. return 0;
  279. }
  280. static int phytium_qspi_write_disable(struct phytium_qspi *qspi)
  281. {
  282. u32 cmd = 0;
  283. cmd = CMD_WRDI << QSPI_CMD_PORT_CMD_SHIFT;
  284. cmd |= BIT(QSPI_CMD_PORT_WAIT_SHIFT);
  285. cmd |= PHYTIUM_SCK_SEL << QSPI_CMD_PORT_SCK_SEL_SHIFT;
  286. writel_relaxed(cmd, qspi->io_base + QSPI_CMD_PORT_REG);
  287. phytium_qspi_cmd_enable(qspi);
  288. return 0;
  289. }
  290. static int phytium_qspi_read_flash_id(struct phytium_qspi *qspi,
  291. u8 opcode, u8 *buf, int len)
  292. {
  293. u32 cmd = 0;
  294. unsigned long iflags;
  295. cmd = opcode << QSPI_CMD_PORT_CMD_SHIFT;
  296. cmd |= BIT(QSPI_CMD_PORT_DATA_TRANSFER_SHIFT);
  297. cmd |= BIT(QSPI_CMD_PORT_P_BUFFER_SHIFT);
  298. cmd |= PHYTIUM_CMD_SCK_SEL << QSPI_CMD_PORT_SCK_SEL_SHIFT;
  299. writel_relaxed(cmd, qspi->io_base + QSPI_CMD_PORT_REG);
  300. phytium_qspi_cmd_enable(qspi);
  301. spin_lock_irqsave(&qspi->spinlock, iflags);
  302. memcpy_from_ftreg(qspi, buf, len);
  303. spin_unlock_irqrestore(&qspi->spinlock, iflags);
  304. return 0;
  305. }
  306. static int phytium_qspi_read_flash_sfdp(struct phytium_qspi *qspi,
  307. u8 opcode, loff_t from, u8 *buf, int len)
  308. {
  309. unsigned long iflags;
  310. u32 cmd = 0;
  311. cmd = opcode << QSPI_CMD_PORT_CMD_SHIFT;
  312. cmd |= BIT(QSPI_CMD_PORT_DATA_TRANSFER_SHIFT);
  313. cmd |= BIT(QSPI_CMD_PORT_P_BUFFER_SHIFT);
  314. cmd |= BIT(QSPI_CMD_PORT_CMD_ADDR_SHIFT);
  315. cmd |= PHYTIUM_SCK_SEL << QSPI_CMD_PORT_SCK_SEL_SHIFT;
  316. writel_relaxed(cmd, qspi->io_base + QSPI_CMD_PORT_REG);
  317. writel_relaxed(from, qspi->io_base + QSPI_ADDR_PORT_REG);
  318. phytium_qspi_cmd_enable(qspi);
  319. spin_lock_irqsave(&qspi->spinlock, iflags);
  320. memcpy_from_ftreg(qspi, buf, len);
  321. spin_unlock_irqrestore(&qspi->spinlock, iflags);
  322. return 0;
  323. }
  324. static int phytium_qspi_read_flash_sr1(struct phytium_qspi *qspi,
  325. u8 opcode, u8 *buf, int len)
  326. {
  327. u32 cmd = 0;
  328. u32 val;
  329. cmd = opcode << QSPI_CMD_PORT_CMD_SHIFT;
  330. cmd |= BIT(QSPI_CMD_PORT_DATA_TRANSFER_SHIFT);
  331. cmd |= (len << QSPI_CMD_PORT_RW_NUM_SHIFT) & QSPI_CMD_PORT_RW_NUM_MASK;
  332. cmd |= PHYTIUM_CMD_SCK_SEL << QSPI_CMD_PORT_SCK_SEL_SHIFT;
  333. writel_relaxed(cmd, qspi->io_base + QSPI_CMD_PORT_REG);
  334. phytium_qspi_cmd_enable(qspi);
  335. val = readl_relaxed(qspi->io_base + QSPI_LD_PORT_REG);
  336. buf[0] = (u8)val;
  337. return 0;
  338. }
  339. static int phytium_qspi_read_reg(struct spi_nor *nor,
  340. u8 opcode, u8 *buf, int len)
  341. {
  342. struct phytium_qspi_flash *flash = nor->priv;
  343. struct device *dev = flash->qspi->dev;
  344. struct phytium_qspi *qspi = flash->qspi;
  345. unsigned long iflags;
  346. u32 cmd = 0;
  347. dev_dbg(dev, "read_reg: cmd:%#.2x buf:%pK len:%#x\n", opcode, buf, len);
  348. switch (opcode) {
  349. case CMD_RDID:
  350. phytium_qspi_read_flash_id(qspi, opcode, buf, len);
  351. return 0;
  352. case CMD_RDSR:
  353. phytium_qspi_read_flash_sr1(qspi, opcode, buf, len);
  354. return 0;
  355. default:
  356. break;
  357. }
  358. cmd = opcode << QSPI_CMD_PORT_CMD_SHIFT;
  359. cmd |= BIT(QSPI_CMD_PORT_DATA_TRANSFER_SHIFT);
  360. cmd |= BIT(QSPI_CMD_PORT_P_BUFFER_SHIFT);
  361. cmd |= PHYTIUM_CMD_SCK_SEL << QSPI_CMD_PORT_SCK_SEL_SHIFT;
  362. writel_relaxed(cmd, qspi->io_base + QSPI_CMD_PORT_REG);
  363. phytium_qspi_cmd_enable(qspi);
  364. spin_lock_irqsave(&qspi->spinlock, iflags);
  365. memcpy_from_ftreg(qspi, buf, len);
  366. spin_unlock_irqrestore(&qspi->spinlock, iflags);
  367. return 0;
  368. }
  369. static int phytium_qspi_write_reg(struct spi_nor *nor, u8 opcode,
  370. u8 *buf, int len)
  371. {
  372. struct phytium_qspi_flash *flash = nor->priv;
  373. struct device *dev = flash->qspi->dev;
  374. struct phytium_qspi *qspi = flash->qspi;
  375. u32 cmd = 0;
  376. dev_dbg(dev, "write_reg: cmd:%#.2x buf:%pK len:%#x\n",
  377. opcode, buf, len);
  378. switch(opcode){
  379. case CMD_WREN:
  380. phytium_qspi_write_enable(qspi);
  381. return 0;
  382. case CMD_WRDI:
  383. phytium_qspi_write_disable(qspi);
  384. return 0;
  385. default:
  386. break;
  387. }
  388. cmd = opcode << QSPI_CMD_PORT_CMD_SHIFT;
  389. cmd |= BIT(QSPI_CMD_PORT_WAIT_SHIFT);
  390. cmd |= PHYTIUM_CMD_SCK_SEL << QSPI_CMD_PORT_SCK_SEL_SHIFT;
  391. if ((len > 8) || (NULL == buf)) {
  392. dev_err(dev, "data length exceed. commad %x, len:%d \n", opcode, len);
  393. return -EINVAL;
  394. }
  395. else if(len > 0){
  396. cmd |= ((len - 1) << QSPI_CMD_PORT_RW_NUM_SHIFT) & QSPI_CMD_PORT_RW_NUM_MASK;
  397. cmd |= BIT(QSPI_CMD_PORT_DATA_TRANSFER_SHIFT);
  398. }
  399. writel_relaxed(cmd, qspi->io_base + QSPI_CMD_PORT_REG);
  400. memcpy_to_ftreg(qspi, buf, len);
  401. return 0;
  402. }
  403. static ssize_t phytium_qspi_read_tmp(struct phytium_qspi *qspi, u32 read_cmd,
  404. loff_t from, size_t len, u_char *buf)
  405. {
  406. u32 addr = (u32)from;
  407. u64 val = 0;
  408. if (!qspi)
  409. return -1;
  410. dev_dbg(qspi->dev, "read cmd:%x, addr:%x len:%zx\n", read_cmd, addr, len);
  411. writel_relaxed(read_cmd, qspi->io_base + QSPI_RD_CFG_REG);
  412. memcpy_fromio(buf, qspi->mm_base + addr, len);
  413. val = *(u64 *)(buf);
  414. dev_dbg(qspi->dev, "read val:%llx\n", val);
  415. return len;
  416. }
  417. static ssize_t phytium_qspi_read(struct spi_nor *nor, loff_t from, size_t len,
  418. u_char *buf)
  419. {
  420. struct phytium_qspi_flash *flash = nor->priv;
  421. struct phytium_qspi *qspi = flash->qspi;
  422. u32 cmd = nor->read_opcode;
  423. u32 addr = (u32)from;
  424. dev_dbg(qspi->dev, "read(%#.2x): buf:%pK from:%#.8x len:%#zx\n",
  425. nor->read_opcode, buf, (u32)from, len);
  426. cmd = cmd << QSPI_RD_CFG_RD_CMD_SHIFT;
  427. cmd |= BIT(QSPI_RD_CFG_D_BUFFER_SHIFT);
  428. cmd |= flash->clk_div << QSPI_CMD_PORT_SCK_SEL_SHIFT;
  429. cmd &= ~QSPI_RD_CFG_RD_TRANSFER_MASK;
  430. cmd |= (flash->addr_width << QSPI_RD_CFG_RD_TRANSFER_SHIFT);
  431. switch (nor->read_opcode) {
  432. case CMD_READ:
  433. case CMD_FAST_READ:
  434. case CMD_QIOR:
  435. case CMD_QOR:
  436. cmd &= ~QSPI_RD_CFG_RD_ADDR_SEL_MASK;
  437. break;
  438. case CMD_4READ:
  439. case CMD_4FAST_READ:
  440. case CMD_4QOR:
  441. case CMD_4QIOR:
  442. cmd |= BIT(QSPI_RD_CFG_RD_ADDR_SEL_SHIFT);
  443. break;
  444. case 0x5A:
  445. cmd &= ~QSPI_RD_CFG_RD_ADDR_SEL_MASK;
  446. phytium_qspi_read_flash_sfdp(qspi, nor->read_opcode, from, buf, len);
  447. return 0;
  448. break;
  449. default:
  450. break;
  451. }
  452. if((PHYTIUM_QSPI_1_1_4 == flash->addr_width) ||
  453. (PHYTIUM_QSPI_1_4_4 == flash->addr_width)) {
  454. cmd |= BIT(QSPI_RD_CFG_RD_LATENCY_SHIFT);
  455. cmd &= ~QSPI_RD_CFG_DUMMY_MASK;
  456. cmd |= (0x07 << QSPI_RD_CFG_DUMMY_SHIFT);
  457. }
  458. dev_dbg(qspi->dev, "read(%#.2x): cmd:%#x\n", nor->read_opcode, cmd);
  459. if (cmd != flash->read_cmd)
  460. flash->read_cmd = cmd;
  461. writel_relaxed(cmd, qspi->io_base + QSPI_RD_CFG_REG);
  462. memcpy_fromio(buf, qspi->mm_base + addr, len);
  463. return len;
  464. }
  465. static ssize_t phytium_qspi_write(struct spi_nor *nor, loff_t to, size_t len,
  466. const u_char *buf)
  467. {
  468. struct phytium_qspi_flash *flash = nor->priv;
  469. struct device *dev = flash->qspi->dev;
  470. struct phytium_qspi *qspi = flash->qspi;
  471. u32 cmd = nor->program_opcode;
  472. u32 addr = (u32)to;
  473. int i;
  474. u_char tmp[8] = {0};
  475. size_t mask = 0x03;
  476. dev_dbg(dev, "write(%#.2x): buf:%p to:%#.8x len:%#zx\n",
  477. nor->program_opcode, buf, (u32)to, len);
  478. if (addr & 0x03) {
  479. dev_err(dev, "Addr not four-byte aligned!\n");
  480. return -EINVAL;
  481. }
  482. cmd = cmd << QSPI_WR_CFG_WR_CMD_SHIFT;
  483. cmd |= BIT(QSPI_WR_CFG_WR_WAIT_SHIFT);
  484. cmd |= BIT(QSPI_WR_CFG_WR_MODE_SHIFT);
  485. cmd |= PHYTIUM_CMD_SCK_SEL << QSPI_CMD_PORT_SCK_SEL_SHIFT;
  486. switch (nor->program_opcode) {
  487. case CMD_PP:
  488. case CMD_QPP:
  489. cmd &= ~QSPI_WR_CFG_WR_ADDR_SEL_MASK;
  490. break;
  491. case CMD_4PP:
  492. case CMD_4QPP:
  493. cmd |= BIT(QSPI_WR_CFG_WR_ADDR_SEL_SHIFT);
  494. break;
  495. default:
  496. dev_err(qspi->dev, "Not support program command:%#x\n",
  497. nor->erase_opcode);
  498. return -EINVAL;
  499. }
  500. dev_dbg(qspi->dev, "write cmd:%x\n", cmd);
  501. writel_relaxed(cmd, qspi->io_base + QSPI_WR_CFG_REG);
  502. for (i = 0; i < len/4; i++) {
  503. writel_relaxed(*(u32 *)(buf + 4*i), qspi->mm_base + addr + 4*i);
  504. }
  505. if (len & mask) {
  506. addr = addr + (len & ~mask);
  507. phytium_qspi_read_tmp(qspi, flash->read_cmd, addr, 4, &tmp[0]);
  508. memcpy(tmp, buf + (len & ~mask), len & mask);
  509. writel_relaxed(*(u32 *)(tmp), qspi->mm_base + addr);
  510. }
  511. writel_relaxed(QSPI_FLUSH_EN, qspi->io_base + QSPI_FLUSH_REG);
  512. phytium_qspi_wait_cmd(qspi);
  513. return len;
  514. }
  515. static int phytium_qspi_erase(struct spi_nor *nor, loff_t offs)
  516. {
  517. struct phytium_qspi_flash *flash = nor->priv;
  518. struct device *dev = flash->qspi->dev;
  519. struct phytium_qspi *qspi = flash->qspi;
  520. u32 cmd = nor->erase_opcode;
  521. u32 addr = (u32)offs;
  522. dev_dbg(dev, "erase(%#.2x):offs:%#x\n", nor->erase_opcode, (u32)offs);
  523. phytium_qspi_write_enable(qspi);
  524. cmd = cmd << QSPI_CMD_PORT_CMD_SHIFT;
  525. cmd |= BIT(QSPI_CMD_PORT_WAIT_SHIFT);
  526. cmd |= PHYTIUM_SCK_SEL << QSPI_CMD_PORT_SCK_SEL_SHIFT;
  527. /* s25fl256s1 not supoort D8, DC, 20, 21 */
  528. switch (nor->erase_opcode) {
  529. case CMD_SE:
  530. cmd &= ~QSPI_CMD_PORT_SEL_MASK;
  531. cmd |= BIT(QSPI_CMD_PORT_CMD_ADDR_SHIFT);
  532. writel_relaxed(addr, qspi->io_base + QSPI_ADDR_PORT_REG);
  533. break;
  534. case CMD_4SE:
  535. cmd |= BIT(QSPI_CMD_PORT_SEL_SHIFT);
  536. cmd |= BIT(QSPI_CMD_PORT_CMD_ADDR_SHIFT);
  537. writel_relaxed(addr, qspi->io_base + QSPI_ADDR_PORT_REG);
  538. break;
  539. case CMD_P4E:
  540. cmd &= ~QSPI_CMD_PORT_SEL_MASK;
  541. cmd |= BIT(QSPI_CMD_PORT_CMD_ADDR_SHIFT);
  542. writel_relaxed(addr, qspi->io_base + QSPI_ADDR_PORT_REG);
  543. break;
  544. case CMD_4P4E:
  545. cmd |= BIT(QSPI_CMD_PORT_SEL_SHIFT);
  546. cmd |= BIT(QSPI_CMD_PORT_CMD_ADDR_SHIFT);
  547. writel_relaxed(addr, qspi->io_base + QSPI_ADDR_PORT_REG);
  548. break;
  549. case CMD_BE:
  550. cmd &= ~QSPI_CMD_PORT_SEL_MASK;
  551. break;
  552. case CMD_4BE:
  553. cmd |= BIT(QSPI_CMD_PORT_SEL_SHIFT);
  554. break;
  555. default:
  556. dev_err(qspi->dev, "Not support erase command:%#x\n",
  557. nor->erase_opcode);
  558. return -EINVAL;
  559. }
  560. writel_relaxed(cmd, qspi->io_base + QSPI_CMD_PORT_REG);
  561. phytium_qspi_cmd_enable(qspi);
  562. phytium_qspi_wait_cmd(qspi);
  563. return 0;
  564. }
  565. static int phytium_qspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  566. {
  567. struct phytium_qspi_flash *flash = nor->priv;
  568. struct phytium_qspi *qspi = flash->qspi;
  569. mutex_lock(&qspi->lock);
  570. return 0;
  571. }
  572. static void phytium_qspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  573. {
  574. struct phytium_qspi_flash *flash = nor->priv;
  575. struct phytium_qspi *qspi = flash->qspi;
  576. mutex_unlock(&qspi->lock);
  577. }
  578. static int phytium_qspi_flash_size_set(struct phytium_qspi *qspi, u32 size)
  579. {
  580. int ret = 0;
  581. u32 value;
  582. switch (size) {
  583. case SZ_4M:
  584. value = 0;
  585. break;
  586. case SZ_8M:
  587. value = 1;
  588. break;
  589. case SZ_16M:
  590. value = 2;
  591. break;
  592. case SZ_32M:
  593. value = 3;
  594. break;
  595. case SZ_64M:
  596. value = 4;
  597. break;
  598. case SZ_128M:
  599. value = 5;
  600. break;
  601. case SZ_256M:
  602. value = 6;
  603. break;
  604. case SZ_512M:
  605. value = 7;
  606. break;
  607. default:
  608. value = 0;
  609. ret = -EINVAL;
  610. return ret;
  611. }
  612. writel_relaxed(value & 0x07, qspi->io_base + QSPI_FLASH_CAP_REG);
  613. return ret;
  614. }
  615. static int phytium_qspi_flash_setup(struct phytium_qspi *qspi,
  616. struct device_node *np)
  617. {
  618. struct spi_nor_hwcaps hwcaps = {
  619. .mask = SNOR_HWCAPS_READ |
  620. SNOR_HWCAPS_READ_FAST |
  621. SNOR_HWCAPS_PP,
  622. };
  623. u32 width, presc;
  624. u32 cs_num = 0;
  625. u32 max_rate = 0;
  626. u32 clk_div = 0;
  627. u32 addr_width = PHYTIUM_QSPI_1_1_1;
  628. struct phytium_qspi_flash *flash;
  629. struct mtd_info *mtd;
  630. int ret;
  631. of_property_read_u32(np, "reg", &cs_num);
  632. if (cs_num >= PHYTIUM_MAX_NORCHIP)
  633. return -EINVAL;
  634. of_property_read_u32(np, "spi-max-frequency", &max_rate);
  635. if (!max_rate)
  636. return -EINVAL;
  637. of_property_read_u32(np, "spi-clk-div", &clk_div);
  638. if (!clk_div)
  639. clk_div = PHYTIUM_SCK_SEL;
  640. if (clk_div < 4)
  641. return -EINVAL;
  642. presc = DIV_ROUND_UP(qspi->clk_rate, max_rate) - 1;
  643. of_property_read_u32(np, "spi-rx-bus-width", &width);
  644. if (!width)
  645. width = 1;
  646. if (width == 4) {
  647. hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
  648. addr_width = PHYTIUM_QSPI_1_1_4;
  649. } else if (width == 2) {
  650. hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
  651. addr_width = PHYTIUM_QSPI_1_1_2;
  652. } else if (width != 1)
  653. return -EINVAL;
  654. flash = &qspi->flash[cs_num];
  655. flash->qspi = qspi;
  656. flash->cs = cs_num;
  657. flash->presc = presc;
  658. flash->clk_div = clk_div;
  659. flash->addr_width = addr_width;
  660. flash->nor.dev = qspi->dev;
  661. spi_nor_set_flash_node(&flash->nor, np);
  662. flash->nor.priv = flash;
  663. mtd = &flash->nor.mtd;
  664. flash->nor.read = phytium_qspi_read;
  665. flash->nor.write = phytium_qspi_write;
  666. flash->nor.erase = phytium_qspi_erase;
  667. flash->nor.read_reg = phytium_qspi_read_reg;
  668. flash->nor.write_reg = phytium_qspi_write_reg;
  669. flash->nor.prepare = phytium_qspi_prep;
  670. flash->nor.unprepare = phytium_qspi_unprep;
  671. flash->fsize = FSIZE_VAL(SZ_1K);
  672. ret = spi_nor_scan(&flash->nor, NULL, &hwcaps);
  673. if (ret) {
  674. dev_err(qspi->dev, "device scan failed\n");
  675. return ret;
  676. }
  677. flash->fsize = FSIZE_VAL(mtd->size);
  678. flash->prefetch_limit = mtd->size - PHYTIUM_QSPI_FIFO_SZ;
  679. ret = phytium_qspi_flash_size_set(flash->qspi, mtd->size);
  680. if (ret) {
  681. dev_err(qspi->dev, "flash size invalid\n");
  682. return ret;
  683. }
  684. flash->read_mode = PHYTIUM_FMODE_MM;
  685. ret = mtd_device_register(mtd, NULL, 0);
  686. if (ret) {
  687. dev_err(qspi->dev, "mtd device parse failed\n");
  688. return ret;
  689. }
  690. flash->registered = true;
  691. dev_dbg(qspi->dev, "read mm:%s %px cs:%d bus:%d clk-div:%d\n",
  692. flash->read_mode == PHYTIUM_FMODE_MM ? "yes" : "no",
  693. qspi->mm_base, cs_num, width, clk_div);
  694. return 0;
  695. }
  696. static void phytium_qspi_mtd_free(struct phytium_qspi *qspi)
  697. {
  698. int i;
  699. for (i = 0; i < PHYTIUM_MAX_NORCHIP; i++)
  700. if (qspi->flash[i].registered)
  701. mtd_device_unregister(&qspi->flash[i].nor.mtd);
  702. }
  703. static ssize_t clk_div_show(struct device *dev, struct device_attribute *attr,
  704. char *buf)
  705. {
  706. struct phytium_qspi *qspi = dev_get_drvdata(dev);
  707. struct phytium_qspi_flash *flash = &qspi->flash[0];
  708. return sprintf(buf, "Flash 0 clk-div: %d\n", flash->clk_div);
  709. }
  710. static ssize_t clk_div_store(struct device *dev,
  711. struct device_attribute *attr, const char *buf,
  712. size_t size)
  713. {
  714. struct phytium_qspi *qspi = dev_get_drvdata(dev);
  715. struct phytium_qspi_flash *flash = &qspi->flash[0];
  716. long value;
  717. char *token;
  718. ssize_t status;
  719. token = strsep ((char **)&buf, " ");
  720. if (!token)
  721. return -EINVAL;
  722. status = kstrtol(token, 0, &value);
  723. if (status)
  724. return status;
  725. flash->clk_div = (u8)value;
  726. return size;
  727. }
  728. static DEVICE_ATTR_RW(clk_div);
  729. static struct attribute *phytium_qspi_attrs[] = {
  730. &dev_attr_clk_div.attr,
  731. NULL,
  732. };
  733. static struct attribute_group phytium_qspi_attr_group = {
  734. .attrs = phytium_qspi_attrs,
  735. };
  736. static int phytium_qspi_probe(struct platform_device *pdev)
  737. {
  738. struct device *dev = &pdev->dev;
  739. struct device_node *flash_np;
  740. struct phytium_qspi *qspi;
  741. struct resource *res;
  742. int ret;
  743. qspi = devm_kzalloc(dev, sizeof(*qspi), GFP_KERNEL);
  744. if (!qspi)
  745. return -ENOMEM;
  746. qspi->nor_num = of_get_child_count(dev->of_node);
  747. if (!qspi->nor_num || qspi->nor_num > PHYTIUM_MAX_NORCHIP)
  748. return -ENODEV;
  749. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi");
  750. qspi->io_base = devm_ioremap_resource(dev, res);
  751. if (IS_ERR(qspi->io_base))
  752. return PTR_ERR(qspi->io_base);
  753. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mm");
  754. qspi->mm_base = devm_ioremap_resource(dev, res);
  755. if (IS_ERR(qspi->mm_base))
  756. return PTR_ERR(qspi->mm_base);
  757. qspi->mm_size = resource_size(res);
  758. qspi->clk = devm_clk_get(dev, NULL);
  759. if (IS_ERR(qspi->clk))
  760. return PTR_ERR(qspi->clk);
  761. qspi->clk_rate = clk_get_rate(qspi->clk);
  762. if (!qspi->clk_rate)
  763. return -EINVAL;
  764. ret = clk_prepare_enable(qspi->clk);
  765. if (ret) {
  766. dev_err(dev, "can not enable the clock\n");
  767. return ret;
  768. }
  769. qspi->dev = dev;
  770. platform_set_drvdata(pdev, qspi);
  771. mutex_init(&qspi->lock);
  772. spin_lock_init(&qspi->spinlock);
  773. for_each_available_child_of_node(dev->of_node, flash_np) {
  774. ret = phytium_qspi_flash_setup(qspi, flash_np);
  775. if (ret) {
  776. dev_err(dev, "unable to setup flash chip\n");
  777. goto err_flash;
  778. }
  779. }
  780. ret = sysfs_create_group(&qspi->dev->kobj, &phytium_qspi_attr_group);
  781. if (ret) {
  782. dev_err(dev, "unable to create sysfs\n");
  783. goto err_flash;
  784. }
  785. return 0;
  786. err_flash:
  787. mutex_destroy(&qspi->lock);
  788. phytium_qspi_mtd_free(qspi);
  789. clk_disable_unprepare(qspi->clk);
  790. return ret;
  791. }
  792. static int phytium_qspi_remove(struct platform_device *pdev)
  793. {
  794. struct phytium_qspi *qspi = platform_get_drvdata(pdev);
  795. sysfs_remove_group(&qspi->dev->kobj, &phytium_qspi_attr_group);
  796. phytium_qspi_mtd_free(qspi);
  797. mutex_destroy(&qspi->lock);
  798. clk_disable_unprepare(qspi->clk);
  799. return 0;
  800. }
  801. static const struct of_device_id phytium_qspi_match[] = {
  802. {.compatible = "phytium,qspi"},
  803. { }
  804. };
  805. MODULE_DEVICE_TABLE(of, phytium_qspi_match);
  806. static struct platform_driver phytium_qspi_driver = {
  807. .probe = phytium_qspi_probe,
  808. .remove = phytium_qspi_remove,
  809. .driver = {
  810. .name = "phytium-quadspi",
  811. .of_match_table = phytium_qspi_match,
  812. },
  813. };
  814. module_platform_driver(phytium_qspi_driver);
  815. MODULE_AUTHOR("Mingshuai Zhu <zhumingshui@phytium.com.cn>");
  816. MODULE_DESCRIPTION("Phytium QuadSPI driver");
  817. MODULE_LICENSE("GPL v2");