spi-orion.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Marvell Orion SPI controller driver
  3. *
  4. * Author: Shadi Ammouri <shadi@marvell.com>
  5. * Copyright (C) 2007-2008 Marvell Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/delay.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/clk.h>
  20. #include <linux/sizes.h>
  21. #include <asm/unaligned.h>
  22. #define DRIVER_NAME "orion_spi"
  23. #define ORION_NUM_CHIPSELECTS 1 /* only one slave is supported*/
  24. #define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
  25. #define ORION_SPI_IF_CTRL_REG 0x00
  26. #define ORION_SPI_IF_CONFIG_REG 0x04
  27. #define ORION_SPI_DATA_OUT_REG 0x08
  28. #define ORION_SPI_DATA_IN_REG 0x0c
  29. #define ORION_SPI_INT_CAUSE_REG 0x10
  30. #define ORION_SPI_MODE_CPOL (1 << 11)
  31. #define ORION_SPI_MODE_CPHA (1 << 12)
  32. #define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
  33. #define ORION_SPI_CLK_PRESCALE_MASK 0x1F
  34. #define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
  35. ORION_SPI_MODE_CPHA)
  36. struct orion_spi {
  37. struct spi_master *master;
  38. void __iomem *base;
  39. struct clk *clk;
  40. };
  41. static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
  42. {
  43. return orion_spi->base + reg;
  44. }
  45. static inline void
  46. orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  47. {
  48. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  49. u32 val;
  50. val = readl(reg_addr);
  51. val |= mask;
  52. writel(val, reg_addr);
  53. }
  54. static inline void
  55. orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  56. {
  57. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  58. u32 val;
  59. val = readl(reg_addr);
  60. val &= ~mask;
  61. writel(val, reg_addr);
  62. }
  63. static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
  64. {
  65. u32 tclk_hz;
  66. u32 rate;
  67. u32 prescale;
  68. u32 reg;
  69. struct orion_spi *orion_spi;
  70. orion_spi = spi_master_get_devdata(spi->master);
  71. tclk_hz = clk_get_rate(orion_spi->clk);
  72. /*
  73. * the supported rates are: 4,6,8...30
  74. * round up as we look for equal or less speed
  75. */
  76. rate = DIV_ROUND_UP(tclk_hz, speed);
  77. rate = roundup(rate, 2);
  78. /* check if requested speed is too small */
  79. if (rate > 30)
  80. return -EINVAL;
  81. if (rate < 4)
  82. rate = 4;
  83. /* Convert the rate to SPI clock divisor value. */
  84. prescale = 0x10 + rate/2;
  85. reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  86. reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | prescale);
  87. writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  88. return 0;
  89. }
  90. static void
  91. orion_spi_mode_set(struct spi_device *spi)
  92. {
  93. u32 reg;
  94. struct orion_spi *orion_spi;
  95. orion_spi = spi_master_get_devdata(spi->master);
  96. reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  97. reg &= ~ORION_SPI_MODE_MASK;
  98. if (spi->mode & SPI_CPOL)
  99. reg |= ORION_SPI_MODE_CPOL;
  100. if (spi->mode & SPI_CPHA)
  101. reg |= ORION_SPI_MODE_CPHA;
  102. writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  103. }
  104. /*
  105. * called only when no transfer is active on the bus
  106. */
  107. static int
  108. orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  109. {
  110. struct orion_spi *orion_spi;
  111. unsigned int speed = spi->max_speed_hz;
  112. unsigned int bits_per_word = spi->bits_per_word;
  113. int rc;
  114. orion_spi = spi_master_get_devdata(spi->master);
  115. if ((t != NULL) && t->speed_hz)
  116. speed = t->speed_hz;
  117. if ((t != NULL) && t->bits_per_word)
  118. bits_per_word = t->bits_per_word;
  119. orion_spi_mode_set(spi);
  120. rc = orion_spi_baudrate_set(spi, speed);
  121. if (rc)
  122. return rc;
  123. if (bits_per_word == 16)
  124. orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  125. ORION_SPI_IF_8_16_BIT_MODE);
  126. else
  127. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  128. ORION_SPI_IF_8_16_BIT_MODE);
  129. return 0;
  130. }
  131. static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable)
  132. {
  133. if (enable)
  134. orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  135. else
  136. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  137. }
  138. static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
  139. {
  140. int i;
  141. for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
  142. if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
  143. return 1;
  144. else
  145. udelay(1);
  146. }
  147. return -1;
  148. }
  149. static inline int
  150. orion_spi_write_read_8bit(struct spi_device *spi,
  151. const u8 **tx_buf, u8 **rx_buf)
  152. {
  153. void __iomem *tx_reg, *rx_reg, *int_reg;
  154. struct orion_spi *orion_spi;
  155. orion_spi = spi_master_get_devdata(spi->master);
  156. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  157. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  158. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  159. /* clear the interrupt cause register */
  160. writel(0x0, int_reg);
  161. if (tx_buf && *tx_buf)
  162. writel(*(*tx_buf)++, tx_reg);
  163. else
  164. writel(0, tx_reg);
  165. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  166. dev_err(&spi->dev, "TXS timed out\n");
  167. return -1;
  168. }
  169. if (rx_buf && *rx_buf)
  170. *(*rx_buf)++ = readl(rx_reg);
  171. return 1;
  172. }
  173. static inline int
  174. orion_spi_write_read_16bit(struct spi_device *spi,
  175. const u16 **tx_buf, u16 **rx_buf)
  176. {
  177. void __iomem *tx_reg, *rx_reg, *int_reg;
  178. struct orion_spi *orion_spi;
  179. orion_spi = spi_master_get_devdata(spi->master);
  180. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  181. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  182. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  183. /* clear the interrupt cause register */
  184. writel(0x0, int_reg);
  185. if (tx_buf && *tx_buf)
  186. writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
  187. else
  188. writel(0, tx_reg);
  189. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  190. dev_err(&spi->dev, "TXS timed out\n");
  191. return -1;
  192. }
  193. if (rx_buf && *rx_buf)
  194. put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
  195. return 1;
  196. }
  197. static unsigned int
  198. orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
  199. {
  200. unsigned int count;
  201. int word_len;
  202. word_len = spi->bits_per_word;
  203. count = xfer->len;
  204. if (word_len == 8) {
  205. const u8 *tx = xfer->tx_buf;
  206. u8 *rx = xfer->rx_buf;
  207. do {
  208. if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
  209. goto out;
  210. count--;
  211. } while (count);
  212. } else if (word_len == 16) {
  213. const u16 *tx = xfer->tx_buf;
  214. u16 *rx = xfer->rx_buf;
  215. do {
  216. if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
  217. goto out;
  218. count -= 2;
  219. } while (count);
  220. }
  221. out:
  222. return xfer->len - count;
  223. }
  224. static int orion_spi_transfer_one_message(struct spi_master *master,
  225. struct spi_message *m)
  226. {
  227. struct orion_spi *orion_spi = spi_master_get_devdata(master);
  228. struct spi_device *spi = m->spi;
  229. struct spi_transfer *t = NULL;
  230. int par_override = 0;
  231. int status = 0;
  232. int cs_active = 0;
  233. /* Load defaults */
  234. status = orion_spi_setup_transfer(spi, NULL);
  235. if (status < 0)
  236. goto msg_done;
  237. list_for_each_entry(t, &m->transfers, transfer_list) {
  238. if (par_override || t->speed_hz || t->bits_per_word) {
  239. par_override = 1;
  240. status = orion_spi_setup_transfer(spi, t);
  241. if (status < 0)
  242. break;
  243. if (!t->speed_hz && !t->bits_per_word)
  244. par_override = 0;
  245. }
  246. if (!cs_active) {
  247. orion_spi_set_cs(orion_spi, 1);
  248. cs_active = 1;
  249. }
  250. if (t->len)
  251. m->actual_length += orion_spi_write_read(spi, t);
  252. if (t->delay_usecs)
  253. udelay(t->delay_usecs);
  254. if (t->cs_change) {
  255. orion_spi_set_cs(orion_spi, 0);
  256. cs_active = 0;
  257. }
  258. }
  259. msg_done:
  260. if (cs_active)
  261. orion_spi_set_cs(orion_spi, 0);
  262. m->status = status;
  263. spi_finalize_current_message(master);
  264. return 0;
  265. }
  266. static int orion_spi_reset(struct orion_spi *orion_spi)
  267. {
  268. /* Verify that the CS is deasserted */
  269. orion_spi_set_cs(orion_spi, 0);
  270. return 0;
  271. }
  272. static int orion_spi_probe(struct platform_device *pdev)
  273. {
  274. struct spi_master *master;
  275. struct orion_spi *spi;
  276. struct resource *r;
  277. unsigned long tclk_hz;
  278. int status = 0;
  279. const u32 *iprop;
  280. int size;
  281. master = spi_alloc_master(&pdev->dev, sizeof(*spi));
  282. if (master == NULL) {
  283. dev_dbg(&pdev->dev, "master allocation failed\n");
  284. return -ENOMEM;
  285. }
  286. if (pdev->id != -1)
  287. master->bus_num = pdev->id;
  288. if (pdev->dev.of_node) {
  289. iprop = of_get_property(pdev->dev.of_node, "cell-index",
  290. &size);
  291. if (iprop && size == sizeof(*iprop))
  292. master->bus_num = *iprop;
  293. }
  294. /* we support only mode 0, and no options */
  295. master->mode_bits = SPI_CPHA | SPI_CPOL;
  296. master->transfer_one_message = orion_spi_transfer_one_message;
  297. master->num_chipselect = ORION_NUM_CHIPSELECTS;
  298. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  299. platform_set_drvdata(pdev, master);
  300. spi = spi_master_get_devdata(master);
  301. spi->master = master;
  302. spi->clk = devm_clk_get(&pdev->dev, NULL);
  303. if (IS_ERR(spi->clk)) {
  304. status = PTR_ERR(spi->clk);
  305. goto out;
  306. }
  307. clk_prepare(spi->clk);
  308. clk_enable(spi->clk);
  309. tclk_hz = clk_get_rate(spi->clk);
  310. master->max_speed_hz = DIV_ROUND_UP(tclk_hz, 4);
  311. master->min_speed_hz = DIV_ROUND_UP(tclk_hz, 30);
  312. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  313. spi->base = devm_ioremap_resource(&pdev->dev, r);
  314. if (IS_ERR(spi->base)) {
  315. status = PTR_ERR(spi->base);
  316. goto out_rel_clk;
  317. }
  318. if (orion_spi_reset(spi) < 0)
  319. goto out_rel_clk;
  320. master->dev.of_node = pdev->dev.of_node;
  321. status = devm_spi_register_master(&pdev->dev, master);
  322. if (status < 0)
  323. goto out_rel_clk;
  324. return status;
  325. out_rel_clk:
  326. clk_disable_unprepare(spi->clk);
  327. out:
  328. spi_master_put(master);
  329. return status;
  330. }
  331. static int orion_spi_remove(struct platform_device *pdev)
  332. {
  333. struct spi_master *master;
  334. struct orion_spi *spi;
  335. master = platform_get_drvdata(pdev);
  336. spi = spi_master_get_devdata(master);
  337. clk_disable_unprepare(spi->clk);
  338. return 0;
  339. }
  340. MODULE_ALIAS("platform:" DRIVER_NAME);
  341. static const struct of_device_id orion_spi_of_match_table[] = {
  342. { .compatible = "marvell,orion-spi", },
  343. {}
  344. };
  345. MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
  346. static struct platform_driver orion_spi_driver = {
  347. .driver = {
  348. .name = DRIVER_NAME,
  349. .owner = THIS_MODULE,
  350. .of_match_table = of_match_ptr(orion_spi_of_match_table),
  351. },
  352. .probe = orion_spi_probe,
  353. .remove = orion_spi_remove,
  354. };
  355. module_platform_driver(orion_spi_driver);
  356. MODULE_DESCRIPTION("Orion SPI driver");
  357. MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
  358. MODULE_LICENSE("GPL");