spi-rb4xx.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * SPI controller driver for the Mikrotik RB4xx boards
  3. *
  4. * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
  5. * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
  6. *
  7. * This file was based on the patches for Linux 2.6.27.39 published by
  8. * MikroTik for their RouterBoard 4xx series devices.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/clk.h>
  19. #include <linux/spi/spi.h>
  20. #include <asm/mach-ath79/ar71xx_regs.h>
  21. struct rb4xx_spi {
  22. void __iomem *base;
  23. struct clk *clk;
  24. };
  25. static inline u32 rb4xx_read(struct rb4xx_spi *rbspi, u32 reg)
  26. {
  27. return __raw_readl(rbspi->base + reg);
  28. }
  29. static inline void rb4xx_write(struct rb4xx_spi *rbspi, u32 reg, u32 value)
  30. {
  31. __raw_writel(value, rbspi->base + reg);
  32. }
  33. static inline void do_spi_clk(struct rb4xx_spi *rbspi, u32 spi_ioc, int value)
  34. {
  35. u32 regval;
  36. regval = spi_ioc;
  37. if (value & BIT(0))
  38. regval |= AR71XX_SPI_IOC_DO;
  39. rb4xx_write(rbspi, AR71XX_SPI_REG_IOC, regval);
  40. rb4xx_write(rbspi, AR71XX_SPI_REG_IOC, regval | AR71XX_SPI_IOC_CLK);
  41. }
  42. static void do_spi_byte(struct rb4xx_spi *rbspi, u32 spi_ioc, u8 byte)
  43. {
  44. int i;
  45. for (i = 7; i >= 0; i--)
  46. do_spi_clk(rbspi, spi_ioc, byte >> i);
  47. }
  48. /* The CS2 pin is used to clock in a second bit per clock cycle. */
  49. static inline void do_spi_clk_two(struct rb4xx_spi *rbspi, u32 spi_ioc,
  50. u8 value)
  51. {
  52. u32 regval;
  53. regval = spi_ioc;
  54. if (value & BIT(1))
  55. regval |= AR71XX_SPI_IOC_DO;
  56. if (value & BIT(0))
  57. regval |= AR71XX_SPI_IOC_CS2;
  58. rb4xx_write(rbspi, AR71XX_SPI_REG_IOC, regval);
  59. rb4xx_write(rbspi, AR71XX_SPI_REG_IOC, regval | AR71XX_SPI_IOC_CLK);
  60. }
  61. /* Two bits at a time, msb first */
  62. static void do_spi_byte_two(struct rb4xx_spi *rbspi, u32 spi_ioc, u8 byte)
  63. {
  64. do_spi_clk_two(rbspi, spi_ioc, byte >> 6);
  65. do_spi_clk_two(rbspi, spi_ioc, byte >> 4);
  66. do_spi_clk_two(rbspi, spi_ioc, byte >> 2);
  67. do_spi_clk_two(rbspi, spi_ioc, byte >> 0);
  68. }
  69. static void rb4xx_set_cs(struct spi_device *spi, bool enable)
  70. {
  71. struct rb4xx_spi *rbspi = spi_master_get_devdata(spi->master);
  72. /*
  73. * Setting CS is done along with bitbanging the actual values,
  74. * since it's all on the same hardware register. However the
  75. * CPLD needs CS deselected after every command.
  76. */
  77. if (enable)
  78. rb4xx_write(rbspi, AR71XX_SPI_REG_IOC,
  79. AR71XX_SPI_IOC_CS0 | AR71XX_SPI_IOC_CS1);
  80. }
  81. static int rb4xx_transfer_one(struct spi_master *master,
  82. struct spi_device *spi, struct spi_transfer *t)
  83. {
  84. struct rb4xx_spi *rbspi = spi_master_get_devdata(master);
  85. int i;
  86. u32 spi_ioc;
  87. u8 *rx_buf;
  88. const u8 *tx_buf;
  89. /*
  90. * Prime the SPI register with the SPI device selected. The m25p80 boot
  91. * flash and CPLD share the CS0 pin. This works because the CPLD's
  92. * command set was designed to almost not clash with that of the
  93. * boot flash.
  94. */
  95. if (spi->chip_select == 2)
  96. /* MMC */
  97. spi_ioc = AR71XX_SPI_IOC_CS0;
  98. else
  99. /* Boot flash and CPLD */
  100. spi_ioc = AR71XX_SPI_IOC_CS1;
  101. tx_buf = t->tx_buf;
  102. rx_buf = t->rx_buf;
  103. for (i = 0; i < t->len; ++i) {
  104. if (t->tx_nbits == SPI_NBITS_DUAL)
  105. /* CPLD can use two-wire transfers */
  106. do_spi_byte_two(rbspi, spi_ioc, tx_buf[i]);
  107. else
  108. do_spi_byte(rbspi, spi_ioc, tx_buf[i]);
  109. if (!rx_buf)
  110. continue;
  111. rx_buf[i] = rb4xx_read(rbspi, AR71XX_SPI_REG_RDS);
  112. }
  113. spi_finalize_current_transfer(master);
  114. return 0;
  115. }
  116. static int rb4xx_spi_probe(struct platform_device *pdev)
  117. {
  118. struct spi_master *master;
  119. struct clk *ahb_clk;
  120. struct rb4xx_spi *rbspi;
  121. struct resource *r;
  122. int err;
  123. void __iomem *spi_base;
  124. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  125. spi_base = devm_ioremap_resource(&pdev->dev, r);
  126. if (IS_ERR(spi_base))
  127. return PTR_ERR(spi_base);
  128. master = spi_alloc_master(&pdev->dev, sizeof(*rbspi));
  129. if (!master)
  130. return -ENOMEM;
  131. ahb_clk = devm_clk_get(&pdev->dev, "ahb");
  132. if (IS_ERR(ahb_clk))
  133. return PTR_ERR(ahb_clk);
  134. master->bus_num = 0;
  135. master->num_chipselect = 3;
  136. master->mode_bits = SPI_TX_DUAL;
  137. master->bits_per_word_mask = BIT(7);
  138. master->flags = SPI_MASTER_MUST_TX;
  139. master->transfer_one = rb4xx_transfer_one;
  140. master->set_cs = rb4xx_set_cs;
  141. err = devm_spi_register_master(&pdev->dev, master);
  142. if (err) {
  143. dev_err(&pdev->dev, "failed to register SPI master\n");
  144. return err;
  145. }
  146. err = clk_prepare_enable(ahb_clk);
  147. if (err)
  148. return err;
  149. rbspi = spi_master_get_devdata(master);
  150. rbspi->base = spi_base;
  151. rbspi->clk = ahb_clk;
  152. platform_set_drvdata(pdev, rbspi);
  153. /* Enable SPI */
  154. rb4xx_write(rbspi, AR71XX_SPI_REG_FS, AR71XX_SPI_FS_GPIO);
  155. return 0;
  156. }
  157. static int rb4xx_spi_remove(struct platform_device *pdev)
  158. {
  159. struct rb4xx_spi *rbspi = platform_get_drvdata(pdev);
  160. clk_disable_unprepare(rbspi->clk);
  161. return 0;
  162. }
  163. static struct platform_driver rb4xx_spi_drv = {
  164. .probe = rb4xx_spi_probe,
  165. .remove = rb4xx_spi_remove,
  166. .driver = {
  167. .name = "rb4xx-spi",
  168. },
  169. };
  170. module_platform_driver(rb4xx_spi_drv);
  171. MODULE_DESCRIPTION("Mikrotik RB4xx SPI controller driver");
  172. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  173. MODULE_AUTHOR("Bert Vermeulen <bert@biot.com>");
  174. MODULE_LICENSE("GPL v2");