spi-meson-spifc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Driver for Amlogic Meson SPI flash controller (SPIFC)
  3. *
  4. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * You should have received a copy of the GNU General Public License
  11. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/regmap.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/types.h>
  25. /* register map */
  26. #define REG_CMD 0x00
  27. #define REG_ADDR 0x04
  28. #define REG_CTRL 0x08
  29. #define REG_CTRL1 0x0c
  30. #define REG_STATUS 0x10
  31. #define REG_CTRL2 0x14
  32. #define REG_CLOCK 0x18
  33. #define REG_USER 0x1c
  34. #define REG_USER1 0x20
  35. #define REG_USER2 0x24
  36. #define REG_USER3 0x28
  37. #define REG_USER4 0x2c
  38. #define REG_SLAVE 0x30
  39. #define REG_SLAVE1 0x34
  40. #define REG_SLAVE2 0x38
  41. #define REG_SLAVE3 0x3c
  42. #define REG_C0 0x40
  43. #define REG_B8 0x60
  44. #define REG_MAX 0x7c
  45. /* register fields */
  46. #define CMD_USER BIT(18)
  47. #define CTRL_ENABLE_AHB BIT(17)
  48. #define CLOCK_SOURCE BIT(31)
  49. #define CLOCK_DIV_SHIFT 12
  50. #define CLOCK_DIV_MASK (0x3f << CLOCK_DIV_SHIFT)
  51. #define CLOCK_CNT_HIGH_SHIFT 6
  52. #define CLOCK_CNT_HIGH_MASK (0x3f << CLOCK_CNT_HIGH_SHIFT)
  53. #define CLOCK_CNT_LOW_SHIFT 0
  54. #define CLOCK_CNT_LOW_MASK (0x3f << CLOCK_CNT_LOW_SHIFT)
  55. #define USER_DIN_EN_MS BIT(0)
  56. #define USER_CMP_MODE BIT(2)
  57. #define USER_UC_DOUT_SEL BIT(27)
  58. #define USER_UC_DIN_SEL BIT(28)
  59. #define USER_UC_MASK ((BIT(5) - 1) << 27)
  60. #define USER1_BN_UC_DOUT_SHIFT 17
  61. #define USER1_BN_UC_DOUT_MASK (0xff << 16)
  62. #define USER1_BN_UC_DIN_SHIFT 8
  63. #define USER1_BN_UC_DIN_MASK (0xff << 8)
  64. #define USER4_CS_ACT BIT(30)
  65. #define SLAVE_TRST_DONE BIT(4)
  66. #define SLAVE_OP_MODE BIT(30)
  67. #define SLAVE_SW_RST BIT(31)
  68. #define SPIFC_BUFFER_SIZE 64
  69. /**
  70. * struct meson_spifc
  71. * @master: the SPI master
  72. * @regmap: regmap for device registers
  73. * @clk: input clock of the built-in baud rate generator
  74. * @device: the device structure
  75. */
  76. struct meson_spifc {
  77. struct spi_master *master;
  78. struct regmap *regmap;
  79. struct clk *clk;
  80. struct device *dev;
  81. };
  82. static const struct regmap_config spifc_regmap_config = {
  83. .reg_bits = 32,
  84. .val_bits = 32,
  85. .reg_stride = 4,
  86. .max_register = REG_MAX,
  87. };
  88. /**
  89. * meson_spifc_wait_ready() - wait for the current operation to terminate
  90. * @spifc: the Meson SPI device
  91. * Return: 0 on success, a negative value on error
  92. */
  93. static int meson_spifc_wait_ready(struct meson_spifc *spifc)
  94. {
  95. unsigned long deadline = jiffies + msecs_to_jiffies(5);
  96. u32 data;
  97. do {
  98. regmap_read(spifc->regmap, REG_SLAVE, &data);
  99. if (data & SLAVE_TRST_DONE)
  100. return 0;
  101. cond_resched();
  102. } while (!time_after(jiffies, deadline));
  103. return -ETIMEDOUT;
  104. }
  105. /**
  106. * meson_spifc_drain_buffer() - copy data from device buffer to memory
  107. * @spifc: the Meson SPI device
  108. * @buf: the destination buffer
  109. * @len: number of bytes to copy
  110. */
  111. static void meson_spifc_drain_buffer(struct meson_spifc *spifc, u8 *buf,
  112. int len)
  113. {
  114. u32 data;
  115. int i = 0;
  116. while (i < len) {
  117. regmap_read(spifc->regmap, REG_C0 + i, &data);
  118. if (len - i >= 4) {
  119. *((u32 *)buf) = data;
  120. buf += 4;
  121. } else {
  122. memcpy(buf, &data, len - i);
  123. break;
  124. }
  125. i += 4;
  126. }
  127. }
  128. /**
  129. * meson_spifc_fill_buffer() - copy data from memory to device buffer
  130. * @spifc: the Meson SPI device
  131. * @buf: the source buffer
  132. * @len: number of bytes to copy
  133. */
  134. static void meson_spifc_fill_buffer(struct meson_spifc *spifc, const u8 *buf,
  135. int len)
  136. {
  137. u32 data;
  138. int i = 0;
  139. while (i < len) {
  140. if (len - i >= 4)
  141. data = *(u32 *)buf;
  142. else
  143. memcpy(&data, buf, len - i);
  144. regmap_write(spifc->regmap, REG_C0 + i, data);
  145. buf += 4;
  146. i += 4;
  147. }
  148. }
  149. /**
  150. * meson_spifc_setup_speed() - program the clock divider
  151. * @spifc: the Meson SPI device
  152. * @speed: desired speed in Hz
  153. */
  154. static void meson_spifc_setup_speed(struct meson_spifc *spifc, u32 speed)
  155. {
  156. unsigned long parent, value;
  157. int n;
  158. parent = clk_get_rate(spifc->clk);
  159. n = max_t(int, parent / speed - 1, 1);
  160. dev_dbg(spifc->dev, "parent %lu, speed %u, n %d\n", parent,
  161. speed, n);
  162. value = (n << CLOCK_DIV_SHIFT) & CLOCK_DIV_MASK;
  163. value |= (n << CLOCK_CNT_LOW_SHIFT) & CLOCK_CNT_LOW_MASK;
  164. value |= (((n + 1) / 2 - 1) << CLOCK_CNT_HIGH_SHIFT) &
  165. CLOCK_CNT_HIGH_MASK;
  166. regmap_write(spifc->regmap, REG_CLOCK, value);
  167. }
  168. /**
  169. * meson_spifc_txrx() - transfer a chunk of data
  170. * @spifc: the Meson SPI device
  171. * @xfer: the current SPI transfer
  172. * @offset: offset of the data to transfer
  173. * @len: length of the data to transfer
  174. * @last_xfer: whether this is the last transfer of the message
  175. * @last_chunk: whether this is the last chunk of the transfer
  176. * Return: 0 on success, a negative value on error
  177. */
  178. static int meson_spifc_txrx(struct meson_spifc *spifc,
  179. struct spi_transfer *xfer,
  180. int offset, int len, bool last_xfer,
  181. bool last_chunk)
  182. {
  183. bool keep_cs = true;
  184. int ret;
  185. if (xfer->tx_buf)
  186. meson_spifc_fill_buffer(spifc, xfer->tx_buf + offset, len);
  187. /* enable DOUT stage */
  188. regmap_update_bits(spifc->regmap, REG_USER, USER_UC_MASK,
  189. USER_UC_DOUT_SEL);
  190. regmap_write(spifc->regmap, REG_USER1,
  191. (8 * len - 1) << USER1_BN_UC_DOUT_SHIFT);
  192. /* enable data input during DOUT */
  193. regmap_update_bits(spifc->regmap, REG_USER, USER_DIN_EN_MS,
  194. USER_DIN_EN_MS);
  195. if (last_chunk) {
  196. if (last_xfer)
  197. keep_cs = xfer->cs_change;
  198. else
  199. keep_cs = !xfer->cs_change;
  200. }
  201. regmap_update_bits(spifc->regmap, REG_USER4, USER4_CS_ACT,
  202. keep_cs ? USER4_CS_ACT : 0);
  203. /* clear transition done bit */
  204. regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_TRST_DONE, 0);
  205. /* start transfer */
  206. regmap_update_bits(spifc->regmap, REG_CMD, CMD_USER, CMD_USER);
  207. ret = meson_spifc_wait_ready(spifc);
  208. if (!ret && xfer->rx_buf)
  209. meson_spifc_drain_buffer(spifc, xfer->rx_buf + offset, len);
  210. return ret;
  211. }
  212. /**
  213. * meson_spifc_transfer_one() - perform a single transfer
  214. * @master: the SPI master
  215. * @spi: the SPI device
  216. * @xfer: the current SPI transfer
  217. * Return: 0 on success, a negative value on error
  218. */
  219. static int meson_spifc_transfer_one(struct spi_master *master,
  220. struct spi_device *spi,
  221. struct spi_transfer *xfer)
  222. {
  223. struct meson_spifc *spifc = spi_master_get_devdata(master);
  224. int len, done = 0, ret = 0;
  225. meson_spifc_setup_speed(spifc, xfer->speed_hz);
  226. regmap_update_bits(spifc->regmap, REG_CTRL, CTRL_ENABLE_AHB, 0);
  227. while (done < xfer->len && !ret) {
  228. len = min_t(int, xfer->len - done, SPIFC_BUFFER_SIZE);
  229. ret = meson_spifc_txrx(spifc, xfer, done, len,
  230. spi_transfer_is_last(master, xfer),
  231. done + len >= xfer->len);
  232. done += len;
  233. }
  234. regmap_update_bits(spifc->regmap, REG_CTRL, CTRL_ENABLE_AHB,
  235. CTRL_ENABLE_AHB);
  236. return ret;
  237. }
  238. /**
  239. * meson_spifc_hw_init() - reset and initialize the SPI controller
  240. * @spifc: the Meson SPI device
  241. */
  242. static void meson_spifc_hw_init(struct meson_spifc *spifc)
  243. {
  244. /* reset device */
  245. regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_SW_RST,
  246. SLAVE_SW_RST);
  247. /* disable compatible mode */
  248. regmap_update_bits(spifc->regmap, REG_USER, USER_CMP_MODE, 0);
  249. /* set master mode */
  250. regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_OP_MODE, 0);
  251. }
  252. static int meson_spifc_probe(struct platform_device *pdev)
  253. {
  254. struct spi_master *master;
  255. struct meson_spifc *spifc;
  256. struct resource *res;
  257. void __iomem *base;
  258. unsigned int rate;
  259. int ret = 0;
  260. master = spi_alloc_master(&pdev->dev, sizeof(struct meson_spifc));
  261. if (!master)
  262. return -ENOMEM;
  263. platform_set_drvdata(pdev, master);
  264. spifc = spi_master_get_devdata(master);
  265. spifc->dev = &pdev->dev;
  266. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  267. base = devm_ioremap_resource(spifc->dev, res);
  268. if (IS_ERR(base)) {
  269. ret = PTR_ERR(base);
  270. goto out_err;
  271. }
  272. spifc->regmap = devm_regmap_init_mmio(spifc->dev, base,
  273. &spifc_regmap_config);
  274. if (IS_ERR(spifc->regmap)) {
  275. ret = PTR_ERR(spifc->regmap);
  276. goto out_err;
  277. }
  278. spifc->clk = devm_clk_get(spifc->dev, NULL);
  279. if (IS_ERR(spifc->clk)) {
  280. dev_err(spifc->dev, "missing clock\n");
  281. ret = PTR_ERR(spifc->clk);
  282. goto out_err;
  283. }
  284. ret = clk_prepare_enable(spifc->clk);
  285. if (ret) {
  286. dev_err(spifc->dev, "can't prepare clock\n");
  287. goto out_err;
  288. }
  289. rate = clk_get_rate(spifc->clk);
  290. master->num_chipselect = 1;
  291. master->dev.of_node = pdev->dev.of_node;
  292. master->bits_per_word_mask = SPI_BPW_MASK(8);
  293. master->auto_runtime_pm = true;
  294. master->transfer_one = meson_spifc_transfer_one;
  295. master->min_speed_hz = rate >> 6;
  296. master->max_speed_hz = rate >> 1;
  297. meson_spifc_hw_init(spifc);
  298. pm_runtime_set_active(spifc->dev);
  299. pm_runtime_enable(spifc->dev);
  300. ret = devm_spi_register_master(spifc->dev, master);
  301. if (ret) {
  302. dev_err(spifc->dev, "failed to register spi master\n");
  303. goto out_clk;
  304. }
  305. return 0;
  306. out_clk:
  307. clk_disable_unprepare(spifc->clk);
  308. out_err:
  309. spi_master_put(master);
  310. return ret;
  311. }
  312. static int meson_spifc_remove(struct platform_device *pdev)
  313. {
  314. struct spi_master *master = platform_get_drvdata(pdev);
  315. struct meson_spifc *spifc = spi_master_get_devdata(master);
  316. pm_runtime_get_sync(&pdev->dev);
  317. clk_disable_unprepare(spifc->clk);
  318. pm_runtime_disable(&pdev->dev);
  319. return 0;
  320. }
  321. #ifdef CONFIG_PM_SLEEP
  322. static int meson_spifc_suspend(struct device *dev)
  323. {
  324. struct spi_master *master = dev_get_drvdata(dev);
  325. struct meson_spifc *spifc = spi_master_get_devdata(master);
  326. int ret;
  327. ret = spi_master_suspend(master);
  328. if (ret)
  329. return ret;
  330. if (!pm_runtime_suspended(dev))
  331. clk_disable_unprepare(spifc->clk);
  332. return 0;
  333. }
  334. static int meson_spifc_resume(struct device *dev)
  335. {
  336. struct spi_master *master = dev_get_drvdata(dev);
  337. struct meson_spifc *spifc = spi_master_get_devdata(master);
  338. int ret;
  339. if (!pm_runtime_suspended(dev)) {
  340. ret = clk_prepare_enable(spifc->clk);
  341. if (ret)
  342. return ret;
  343. }
  344. meson_spifc_hw_init(spifc);
  345. ret = spi_master_resume(master);
  346. if (ret)
  347. clk_disable_unprepare(spifc->clk);
  348. return ret;
  349. }
  350. #endif /* CONFIG_PM_SLEEP */
  351. #ifdef CONFIG_PM
  352. static int meson_spifc_runtime_suspend(struct device *dev)
  353. {
  354. struct spi_master *master = dev_get_drvdata(dev);
  355. struct meson_spifc *spifc = spi_master_get_devdata(master);
  356. clk_disable_unprepare(spifc->clk);
  357. return 0;
  358. }
  359. static int meson_spifc_runtime_resume(struct device *dev)
  360. {
  361. struct spi_master *master = dev_get_drvdata(dev);
  362. struct meson_spifc *spifc = spi_master_get_devdata(master);
  363. return clk_prepare_enable(spifc->clk);
  364. }
  365. #endif /* CONFIG_PM */
  366. static const struct dev_pm_ops meson_spifc_pm_ops = {
  367. SET_SYSTEM_SLEEP_PM_OPS(meson_spifc_suspend, meson_spifc_resume)
  368. SET_RUNTIME_PM_OPS(meson_spifc_runtime_suspend,
  369. meson_spifc_runtime_resume,
  370. NULL)
  371. };
  372. static const struct of_device_id meson_spifc_dt_match[] = {
  373. { .compatible = "amlogic,meson6-spifc", },
  374. { .compatible = "amlogic,meson-gxbb-spifc", },
  375. { },
  376. };
  377. MODULE_DEVICE_TABLE(of, meson_spifc_dt_match);
  378. static struct platform_driver meson_spifc_driver = {
  379. .probe = meson_spifc_probe,
  380. .remove = meson_spifc_remove,
  381. .driver = {
  382. .name = "meson-spifc",
  383. .of_match_table = of_match_ptr(meson_spifc_dt_match),
  384. .pm = &meson_spifc_pm_ops,
  385. },
  386. };
  387. module_platform_driver(meson_spifc_driver);
  388. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  389. MODULE_DESCRIPTION("Amlogic Meson SPIFC driver");
  390. MODULE_LICENSE("GPL v2");