spi-orion.c 11 KB

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