spi-octeon.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2011, 2012 Cavium, Inc.
  7. */
  8. #include <linux/platform_device.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/of.h>
  16. #include <asm/octeon/octeon.h>
  17. #include <asm/octeon/cvmx-mpi-defs.h>
  18. #define OCTEON_SPI_CFG 0
  19. #define OCTEON_SPI_STS 0x08
  20. #define OCTEON_SPI_TX 0x10
  21. #define OCTEON_SPI_DAT0 0x80
  22. #define OCTEON_SPI_MAX_BYTES 9
  23. #define OCTEON_SPI_MAX_CLOCK_HZ 16000000
  24. struct octeon_spi {
  25. u64 register_base;
  26. u64 last_cfg;
  27. u64 cs_enax;
  28. };
  29. struct octeon_spi_setup {
  30. u32 max_speed_hz;
  31. u8 chip_select;
  32. u8 mode;
  33. u8 bits_per_word;
  34. };
  35. static void octeon_spi_wait_ready(struct octeon_spi *p)
  36. {
  37. union cvmx_mpi_sts mpi_sts;
  38. unsigned int loops = 0;
  39. do {
  40. if (loops++)
  41. __delay(500);
  42. mpi_sts.u64 = cvmx_read_csr(p->register_base + OCTEON_SPI_STS);
  43. } while (mpi_sts.s.busy);
  44. }
  45. static int octeon_spi_do_transfer(struct octeon_spi *p,
  46. struct spi_message *msg,
  47. struct spi_transfer *xfer,
  48. bool last_xfer)
  49. {
  50. union cvmx_mpi_cfg mpi_cfg;
  51. union cvmx_mpi_tx mpi_tx;
  52. unsigned int clkdiv;
  53. unsigned int speed_hz;
  54. int mode;
  55. bool cpha, cpol;
  56. int bits_per_word;
  57. const u8 *tx_buf;
  58. u8 *rx_buf;
  59. int len;
  60. int i;
  61. struct octeon_spi_setup *msg_setup = spi_get_ctldata(msg->spi);
  62. speed_hz = msg_setup->max_speed_hz;
  63. mode = msg_setup->mode;
  64. cpha = mode & SPI_CPHA;
  65. cpol = mode & SPI_CPOL;
  66. bits_per_word = msg_setup->bits_per_word;
  67. if (xfer->speed_hz)
  68. speed_hz = xfer->speed_hz;
  69. if (xfer->bits_per_word)
  70. bits_per_word = xfer->bits_per_word;
  71. if (speed_hz > OCTEON_SPI_MAX_CLOCK_HZ)
  72. speed_hz = OCTEON_SPI_MAX_CLOCK_HZ;
  73. clkdiv = octeon_get_io_clock_rate() / (2 * speed_hz);
  74. mpi_cfg.u64 = 0;
  75. mpi_cfg.s.clkdiv = clkdiv;
  76. mpi_cfg.s.cshi = (mode & SPI_CS_HIGH) ? 1 : 0;
  77. mpi_cfg.s.lsbfirst = (mode & SPI_LSB_FIRST) ? 1 : 0;
  78. mpi_cfg.s.wireor = (mode & SPI_3WIRE) ? 1 : 0;
  79. mpi_cfg.s.idlelo = cpha != cpol;
  80. mpi_cfg.s.cslate = cpha ? 1 : 0;
  81. mpi_cfg.s.enable = 1;
  82. if (msg_setup->chip_select < 4)
  83. p->cs_enax |= 1ull << (12 + msg_setup->chip_select);
  84. mpi_cfg.u64 |= p->cs_enax;
  85. if (mpi_cfg.u64 != p->last_cfg) {
  86. p->last_cfg = mpi_cfg.u64;
  87. cvmx_write_csr(p->register_base + OCTEON_SPI_CFG, mpi_cfg.u64);
  88. }
  89. tx_buf = xfer->tx_buf;
  90. rx_buf = xfer->rx_buf;
  91. len = xfer->len;
  92. while (len > OCTEON_SPI_MAX_BYTES) {
  93. for (i = 0; i < OCTEON_SPI_MAX_BYTES; i++) {
  94. u8 d;
  95. if (tx_buf)
  96. d = *tx_buf++;
  97. else
  98. d = 0;
  99. cvmx_write_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i), d);
  100. }
  101. mpi_tx.u64 = 0;
  102. mpi_tx.s.csid = msg_setup->chip_select;
  103. mpi_tx.s.leavecs = 1;
  104. mpi_tx.s.txnum = tx_buf ? OCTEON_SPI_MAX_BYTES : 0;
  105. mpi_tx.s.totnum = OCTEON_SPI_MAX_BYTES;
  106. cvmx_write_csr(p->register_base + OCTEON_SPI_TX, mpi_tx.u64);
  107. octeon_spi_wait_ready(p);
  108. if (rx_buf)
  109. for (i = 0; i < OCTEON_SPI_MAX_BYTES; i++) {
  110. u64 v = cvmx_read_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i));
  111. *rx_buf++ = (u8)v;
  112. }
  113. len -= OCTEON_SPI_MAX_BYTES;
  114. }
  115. for (i = 0; i < len; i++) {
  116. u8 d;
  117. if (tx_buf)
  118. d = *tx_buf++;
  119. else
  120. d = 0;
  121. cvmx_write_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i), d);
  122. }
  123. mpi_tx.u64 = 0;
  124. mpi_tx.s.csid = msg_setup->chip_select;
  125. if (last_xfer)
  126. mpi_tx.s.leavecs = xfer->cs_change;
  127. else
  128. mpi_tx.s.leavecs = !xfer->cs_change;
  129. mpi_tx.s.txnum = tx_buf ? len : 0;
  130. mpi_tx.s.totnum = len;
  131. cvmx_write_csr(p->register_base + OCTEON_SPI_TX, mpi_tx.u64);
  132. octeon_spi_wait_ready(p);
  133. if (rx_buf)
  134. for (i = 0; i < len; i++) {
  135. u64 v = cvmx_read_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i));
  136. *rx_buf++ = (u8)v;
  137. }
  138. if (xfer->delay_usecs)
  139. udelay(xfer->delay_usecs);
  140. return xfer->len;
  141. }
  142. static int octeon_spi_validate_bpw(struct spi_device *spi, u32 speed)
  143. {
  144. switch (speed) {
  145. case 8:
  146. break;
  147. default:
  148. dev_err(&spi->dev, "Error: %d bits per word not supported\n",
  149. speed);
  150. return -EINVAL;
  151. }
  152. return 0;
  153. }
  154. static int octeon_spi_transfer_one_message(struct spi_master *master,
  155. struct spi_message *msg)
  156. {
  157. struct octeon_spi *p = spi_master_get_devdata(master);
  158. unsigned int total_len = 0;
  159. int status = 0;
  160. struct spi_transfer *xfer;
  161. /*
  162. * We better have set the configuration via a call to .setup
  163. * before we get here.
  164. */
  165. if (spi_get_ctldata(msg->spi) == NULL) {
  166. status = -EINVAL;
  167. goto err;
  168. }
  169. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  170. if (xfer->bits_per_word) {
  171. status = octeon_spi_validate_bpw(msg->spi,
  172. xfer->bits_per_word);
  173. if (status)
  174. goto err;
  175. }
  176. }
  177. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  178. bool last_xfer = &xfer->transfer_list == msg->transfers.prev;
  179. int r = octeon_spi_do_transfer(p, msg, xfer, last_xfer);
  180. if (r < 0) {
  181. status = r;
  182. goto err;
  183. }
  184. total_len += r;
  185. }
  186. err:
  187. msg->status = status;
  188. msg->actual_length = total_len;
  189. spi_finalize_current_message(master);
  190. return status;
  191. }
  192. static struct octeon_spi_setup *octeon_spi_new_setup(struct spi_device *spi)
  193. {
  194. struct octeon_spi_setup *setup = kzalloc(sizeof(*setup), GFP_KERNEL);
  195. if (!setup)
  196. return NULL;
  197. setup->max_speed_hz = spi->max_speed_hz;
  198. setup->chip_select = spi->chip_select;
  199. setup->mode = spi->mode;
  200. setup->bits_per_word = spi->bits_per_word;
  201. return setup;
  202. }
  203. static int octeon_spi_setup(struct spi_device *spi)
  204. {
  205. int r;
  206. struct octeon_spi_setup *new_setup;
  207. struct octeon_spi_setup *old_setup = spi_get_ctldata(spi);
  208. r = octeon_spi_validate_bpw(spi, spi->bits_per_word);
  209. if (r)
  210. return r;
  211. new_setup = octeon_spi_new_setup(spi);
  212. if (!new_setup)
  213. return -ENOMEM;
  214. spi_set_ctldata(spi, new_setup);
  215. kfree(old_setup);
  216. return 0;
  217. }
  218. static void octeon_spi_cleanup(struct spi_device *spi)
  219. {
  220. struct octeon_spi_setup *old_setup = spi_get_ctldata(spi);
  221. spi_set_ctldata(spi, NULL);
  222. kfree(old_setup);
  223. }
  224. static int octeon_spi_nop_transfer_hardware(struct spi_master *master)
  225. {
  226. return 0;
  227. }
  228. static int octeon_spi_probe(struct platform_device *pdev)
  229. {
  230. struct resource *res_mem;
  231. struct spi_master *master;
  232. struct octeon_spi *p;
  233. int err = -ENOENT;
  234. master = spi_alloc_master(&pdev->dev, sizeof(struct octeon_spi));
  235. if (!master)
  236. return -ENOMEM;
  237. p = spi_master_get_devdata(master);
  238. platform_set_drvdata(pdev, master);
  239. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  240. if (res_mem == NULL) {
  241. dev_err(&pdev->dev, "found no memory resource\n");
  242. err = -ENXIO;
  243. goto fail;
  244. }
  245. if (!devm_request_mem_region(&pdev->dev, res_mem->start,
  246. resource_size(res_mem), res_mem->name)) {
  247. dev_err(&pdev->dev, "request_mem_region failed\n");
  248. goto fail;
  249. }
  250. p->register_base = (u64)devm_ioremap(&pdev->dev, res_mem->start,
  251. resource_size(res_mem));
  252. /* Dynamic bus numbering */
  253. master->bus_num = -1;
  254. master->num_chipselect = 4;
  255. master->mode_bits = SPI_CPHA |
  256. SPI_CPOL |
  257. SPI_CS_HIGH |
  258. SPI_LSB_FIRST |
  259. SPI_3WIRE;
  260. master->setup = octeon_spi_setup;
  261. master->cleanup = octeon_spi_cleanup;
  262. master->prepare_transfer_hardware = octeon_spi_nop_transfer_hardware;
  263. master->transfer_one_message = octeon_spi_transfer_one_message;
  264. master->unprepare_transfer_hardware = octeon_spi_nop_transfer_hardware;
  265. master->dev.of_node = pdev->dev.of_node;
  266. err = spi_register_master(master);
  267. if (err) {
  268. dev_err(&pdev->dev, "register master failed: %d\n", err);
  269. goto fail;
  270. }
  271. dev_info(&pdev->dev, "OCTEON SPI bus driver\n");
  272. return 0;
  273. fail:
  274. spi_master_put(master);
  275. return err;
  276. }
  277. static int octeon_spi_remove(struct platform_device *pdev)
  278. {
  279. struct spi_master *master = platform_get_drvdata(pdev);
  280. struct octeon_spi *p = spi_master_get_devdata(master);
  281. u64 register_base = p->register_base;
  282. spi_unregister_master(master);
  283. /* Clear the CSENA* and put everything in a known state. */
  284. cvmx_write_csr(register_base + OCTEON_SPI_CFG, 0);
  285. return 0;
  286. }
  287. static struct of_device_id octeon_spi_match[] = {
  288. { .compatible = "cavium,octeon-3010-spi", },
  289. {},
  290. };
  291. MODULE_DEVICE_TABLE(of, octeon_spi_match);
  292. static struct platform_driver octeon_spi_driver = {
  293. .driver = {
  294. .name = "spi-octeon",
  295. .owner = THIS_MODULE,
  296. .of_match_table = octeon_spi_match,
  297. },
  298. .probe = octeon_spi_probe,
  299. .remove = octeon_spi_remove,
  300. };
  301. module_platform_driver(octeon_spi_driver);
  302. MODULE_DESCRIPTION("Cavium, Inc. OCTEON SPI bus driver");
  303. MODULE_AUTHOR("David Daney");
  304. MODULE_LICENSE("GPL");