spi-orion.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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/pm_runtime.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/clk.h>
  24. #include <linux/sizes.h>
  25. #include <linux/gpio.h>
  26. #include <asm/unaligned.h>
  27. #define DRIVER_NAME "orion_spi"
  28. /* Runtime PM autosuspend timeout: PM is fairly light on this driver */
  29. #define SPI_AUTOSUSPEND_TIMEOUT 200
  30. /* Some SoCs using this driver support up to 8 chip selects.
  31. * It is up to the implementer to only use the chip selects
  32. * that are available.
  33. */
  34. #define ORION_NUM_CHIPSELECTS 8
  35. #define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
  36. #define ORION_SPI_IF_CTRL_REG 0x00
  37. #define ORION_SPI_IF_CONFIG_REG 0x04
  38. #define ORION_SPI_IF_RXLSBF BIT(14)
  39. #define ORION_SPI_IF_TXLSBF BIT(13)
  40. #define ORION_SPI_DATA_OUT_REG 0x08
  41. #define ORION_SPI_DATA_IN_REG 0x0c
  42. #define ORION_SPI_INT_CAUSE_REG 0x10
  43. #define ORION_SPI_TIMING_PARAMS_REG 0x18
  44. /* Register for the "Direct Mode" */
  45. #define SPI_DIRECT_WRITE_CONFIG_REG 0x20
  46. #define ORION_SPI_TMISO_SAMPLE_MASK (0x3 << 6)
  47. #define ORION_SPI_TMISO_SAMPLE_1 (1 << 6)
  48. #define ORION_SPI_TMISO_SAMPLE_2 (2 << 6)
  49. #define ORION_SPI_MODE_CPOL (1 << 11)
  50. #define ORION_SPI_MODE_CPHA (1 << 12)
  51. #define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
  52. #define ORION_SPI_CLK_PRESCALE_MASK 0x1F
  53. #define ARMADA_SPI_CLK_PRESCALE_MASK 0xDF
  54. #define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
  55. ORION_SPI_MODE_CPHA)
  56. #define ORION_SPI_CS_MASK 0x1C
  57. #define ORION_SPI_CS_SHIFT 2
  58. #define ORION_SPI_CS(cs) ((cs << ORION_SPI_CS_SHIFT) & \
  59. ORION_SPI_CS_MASK)
  60. enum orion_spi_type {
  61. ORION_SPI,
  62. ARMADA_SPI,
  63. };
  64. struct orion_spi_dev {
  65. enum orion_spi_type typ;
  66. /*
  67. * min_divisor and max_hz should be exclusive, the only we can
  68. * have both is for managing the armada-370-spi case with old
  69. * device tree
  70. */
  71. unsigned long max_hz;
  72. unsigned int min_divisor;
  73. unsigned int max_divisor;
  74. u32 prescale_mask;
  75. bool is_errata_50mhz_ac;
  76. };
  77. struct orion_direct_acc {
  78. void __iomem *vaddr;
  79. u32 size;
  80. };
  81. struct orion_child_options {
  82. struct orion_direct_acc direct_access;
  83. };
  84. struct orion_spi {
  85. struct spi_master *master;
  86. void __iomem *base;
  87. struct clk *clk;
  88. struct clk *axi_clk;
  89. const struct orion_spi_dev *devdata;
  90. int unused_hw_gpio;
  91. struct orion_child_options child[ORION_NUM_CHIPSELECTS];
  92. };
  93. static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
  94. {
  95. return orion_spi->base + reg;
  96. }
  97. static inline void
  98. orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  99. {
  100. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  101. u32 val;
  102. val = readl(reg_addr);
  103. val |= mask;
  104. writel(val, reg_addr);
  105. }
  106. static inline void
  107. orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  108. {
  109. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  110. u32 val;
  111. val = readl(reg_addr);
  112. val &= ~mask;
  113. writel(val, reg_addr);
  114. }
  115. static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
  116. {
  117. u32 tclk_hz;
  118. u32 rate;
  119. u32 prescale;
  120. u32 reg;
  121. struct orion_spi *orion_spi;
  122. const struct orion_spi_dev *devdata;
  123. orion_spi = spi_master_get_devdata(spi->master);
  124. devdata = orion_spi->devdata;
  125. tclk_hz = clk_get_rate(orion_spi->clk);
  126. if (devdata->typ == ARMADA_SPI) {
  127. /*
  128. * Given the core_clk (tclk_hz) and the target rate (speed) we
  129. * determine the best values for SPR (in [0 .. 15]) and SPPR (in
  130. * [0..7]) such that
  131. *
  132. * core_clk / (SPR * 2 ** SPPR)
  133. *
  134. * is as big as possible but not bigger than speed.
  135. */
  136. /* best integer divider: */
  137. unsigned divider = DIV_ROUND_UP(tclk_hz, speed);
  138. unsigned spr, sppr;
  139. if (divider < 16) {
  140. /* This is the easy case, divider is less than 16 */
  141. spr = divider;
  142. sppr = 0;
  143. } else {
  144. unsigned two_pow_sppr;
  145. /*
  146. * Find the highest bit set in divider. This and the
  147. * three next bits define SPR (apart from rounding).
  148. * SPPR is then the number of zero bits that must be
  149. * appended:
  150. */
  151. sppr = fls(divider) - 4;
  152. /*
  153. * As SPR only has 4 bits, we have to round divider up
  154. * to the next multiple of 2 ** sppr.
  155. */
  156. two_pow_sppr = 1 << sppr;
  157. divider = (divider + two_pow_sppr - 1) & -two_pow_sppr;
  158. /*
  159. * recalculate sppr as rounding up divider might have
  160. * increased it enough to change the position of the
  161. * highest set bit. In this case the bit that now
  162. * doesn't make it into SPR is 0, so there is no need to
  163. * round again.
  164. */
  165. sppr = fls(divider) - 4;
  166. spr = divider >> sppr;
  167. /*
  168. * Now do range checking. SPR is constructed to have a
  169. * width of 4 bits, so this is fine for sure. So we
  170. * still need to check for sppr to fit into 3 bits:
  171. */
  172. if (sppr > 7)
  173. return -EINVAL;
  174. }
  175. prescale = ((sppr & 0x6) << 5) | ((sppr & 0x1) << 4) | spr;
  176. } else {
  177. /*
  178. * the supported rates are: 4,6,8...30
  179. * round up as we look for equal or less speed
  180. */
  181. rate = DIV_ROUND_UP(tclk_hz, speed);
  182. rate = roundup(rate, 2);
  183. /* check if requested speed is too small */
  184. if (rate > 30)
  185. return -EINVAL;
  186. if (rate < 4)
  187. rate = 4;
  188. /* Convert the rate to SPI clock divisor value. */
  189. prescale = 0x10 + rate/2;
  190. }
  191. reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  192. reg = ((reg & ~devdata->prescale_mask) | prescale);
  193. writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  194. return 0;
  195. }
  196. static void
  197. orion_spi_mode_set(struct spi_device *spi)
  198. {
  199. u32 reg;
  200. struct orion_spi *orion_spi;
  201. orion_spi = spi_master_get_devdata(spi->master);
  202. reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  203. reg &= ~ORION_SPI_MODE_MASK;
  204. if (spi->mode & SPI_CPOL)
  205. reg |= ORION_SPI_MODE_CPOL;
  206. if (spi->mode & SPI_CPHA)
  207. reg |= ORION_SPI_MODE_CPHA;
  208. if (spi->mode & SPI_LSB_FIRST)
  209. reg |= ORION_SPI_IF_RXLSBF | ORION_SPI_IF_TXLSBF;
  210. else
  211. reg &= ~(ORION_SPI_IF_RXLSBF | ORION_SPI_IF_TXLSBF);
  212. writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  213. }
  214. static void
  215. orion_spi_50mhz_ac_timing_erratum(struct spi_device *spi, unsigned int speed)
  216. {
  217. u32 reg;
  218. struct orion_spi *orion_spi;
  219. orion_spi = spi_master_get_devdata(spi->master);
  220. /*
  221. * Erratum description: (Erratum NO. FE-9144572) The device
  222. * SPI interface supports frequencies of up to 50 MHz.
  223. * However, due to this erratum, when the device core clock is
  224. * 250 MHz and the SPI interfaces is configured for 50MHz SPI
  225. * clock and CPOL=CPHA=1 there might occur data corruption on
  226. * reads from the SPI device.
  227. * Erratum Workaround:
  228. * Work in one of the following configurations:
  229. * 1. Set CPOL=CPHA=0 in "SPI Interface Configuration
  230. * Register".
  231. * 2. Set TMISO_SAMPLE value to 0x2 in "SPI Timing Parameters 1
  232. * Register" before setting the interface.
  233. */
  234. reg = readl(spi_reg(orion_spi, ORION_SPI_TIMING_PARAMS_REG));
  235. reg &= ~ORION_SPI_TMISO_SAMPLE_MASK;
  236. if (clk_get_rate(orion_spi->clk) == 250000000 &&
  237. speed == 50000000 && spi->mode & SPI_CPOL &&
  238. spi->mode & SPI_CPHA)
  239. reg |= ORION_SPI_TMISO_SAMPLE_2;
  240. else
  241. reg |= ORION_SPI_TMISO_SAMPLE_1; /* This is the default value */
  242. writel(reg, spi_reg(orion_spi, ORION_SPI_TIMING_PARAMS_REG));
  243. }
  244. /*
  245. * called only when no transfer is active on the bus
  246. */
  247. static int
  248. orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  249. {
  250. struct orion_spi *orion_spi;
  251. unsigned int speed = spi->max_speed_hz;
  252. unsigned int bits_per_word = spi->bits_per_word;
  253. int rc;
  254. orion_spi = spi_master_get_devdata(spi->master);
  255. if ((t != NULL) && t->speed_hz)
  256. speed = t->speed_hz;
  257. if ((t != NULL) && t->bits_per_word)
  258. bits_per_word = t->bits_per_word;
  259. orion_spi_mode_set(spi);
  260. if (orion_spi->devdata->is_errata_50mhz_ac)
  261. orion_spi_50mhz_ac_timing_erratum(spi, speed);
  262. rc = orion_spi_baudrate_set(spi, speed);
  263. if (rc)
  264. return rc;
  265. if (bits_per_word == 16)
  266. orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  267. ORION_SPI_IF_8_16_BIT_MODE);
  268. else
  269. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  270. ORION_SPI_IF_8_16_BIT_MODE);
  271. return 0;
  272. }
  273. static void orion_spi_set_cs(struct spi_device *spi, bool enable)
  274. {
  275. struct orion_spi *orion_spi;
  276. int cs;
  277. orion_spi = spi_master_get_devdata(spi->master);
  278. if (gpio_is_valid(spi->cs_gpio))
  279. cs = orion_spi->unused_hw_gpio;
  280. else
  281. cs = spi->chip_select;
  282. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, ORION_SPI_CS_MASK);
  283. orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG,
  284. ORION_SPI_CS(cs));
  285. /* Chip select logic is inverted from spi_set_cs */
  286. if (!enable)
  287. orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  288. else
  289. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  290. }
  291. static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
  292. {
  293. int i;
  294. for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
  295. if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
  296. return 1;
  297. udelay(1);
  298. }
  299. return -1;
  300. }
  301. static inline int
  302. orion_spi_write_read_8bit(struct spi_device *spi,
  303. const u8 **tx_buf, u8 **rx_buf)
  304. {
  305. void __iomem *tx_reg, *rx_reg, *int_reg;
  306. struct orion_spi *orion_spi;
  307. orion_spi = spi_master_get_devdata(spi->master);
  308. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  309. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  310. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  311. /* clear the interrupt cause register */
  312. writel(0x0, int_reg);
  313. if (tx_buf && *tx_buf)
  314. writel(*(*tx_buf)++, tx_reg);
  315. else
  316. writel(0, tx_reg);
  317. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  318. dev_err(&spi->dev, "TXS timed out\n");
  319. return -1;
  320. }
  321. if (rx_buf && *rx_buf)
  322. *(*rx_buf)++ = readl(rx_reg);
  323. return 1;
  324. }
  325. static inline int
  326. orion_spi_write_read_16bit(struct spi_device *spi,
  327. const u16 **tx_buf, u16 **rx_buf)
  328. {
  329. void __iomem *tx_reg, *rx_reg, *int_reg;
  330. struct orion_spi *orion_spi;
  331. orion_spi = spi_master_get_devdata(spi->master);
  332. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  333. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  334. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  335. /* clear the interrupt cause register */
  336. writel(0x0, int_reg);
  337. if (tx_buf && *tx_buf)
  338. writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
  339. else
  340. writel(0, tx_reg);
  341. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  342. dev_err(&spi->dev, "TXS timed out\n");
  343. return -1;
  344. }
  345. if (rx_buf && *rx_buf)
  346. put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
  347. return 1;
  348. }
  349. static unsigned int
  350. orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
  351. {
  352. unsigned int count;
  353. int word_len;
  354. struct orion_spi *orion_spi;
  355. int cs = spi->chip_select;
  356. word_len = spi->bits_per_word;
  357. count = xfer->len;
  358. orion_spi = spi_master_get_devdata(spi->master);
  359. /*
  360. * Use SPI direct write mode if base address is available. Otherwise
  361. * fall back to PIO mode for this transfer.
  362. */
  363. if ((orion_spi->child[cs].direct_access.vaddr) && (xfer->tx_buf) &&
  364. (word_len == 8)) {
  365. unsigned int cnt = count / 4;
  366. unsigned int rem = count % 4;
  367. /*
  368. * Send the TX-data to the SPI device via the direct
  369. * mapped address window
  370. */
  371. iowrite32_rep(orion_spi->child[cs].direct_access.vaddr,
  372. xfer->tx_buf, cnt);
  373. if (rem) {
  374. u32 *buf = (u32 *)xfer->tx_buf;
  375. iowrite8_rep(orion_spi->child[cs].direct_access.vaddr,
  376. &buf[cnt], rem);
  377. }
  378. return count;
  379. }
  380. if (word_len == 8) {
  381. const u8 *tx = xfer->tx_buf;
  382. u8 *rx = xfer->rx_buf;
  383. do {
  384. if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
  385. goto out;
  386. count--;
  387. } while (count);
  388. } else if (word_len == 16) {
  389. const u16 *tx = xfer->tx_buf;
  390. u16 *rx = xfer->rx_buf;
  391. do {
  392. if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
  393. goto out;
  394. count -= 2;
  395. } while (count);
  396. }
  397. out:
  398. return xfer->len - count;
  399. }
  400. static int orion_spi_transfer_one(struct spi_master *master,
  401. struct spi_device *spi,
  402. struct spi_transfer *t)
  403. {
  404. int status = 0;
  405. status = orion_spi_setup_transfer(spi, t);
  406. if (status < 0)
  407. return status;
  408. if (t->len)
  409. orion_spi_write_read(spi, t);
  410. return status;
  411. }
  412. static int orion_spi_setup(struct spi_device *spi)
  413. {
  414. if (gpio_is_valid(spi->cs_gpio)) {
  415. gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  416. }
  417. return orion_spi_setup_transfer(spi, NULL);
  418. }
  419. static int orion_spi_reset(struct orion_spi *orion_spi)
  420. {
  421. /* Verify that the CS is deasserted */
  422. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  423. /* Don't deassert CS between the direct mapped SPI transfers */
  424. writel(0, spi_reg(orion_spi, SPI_DIRECT_WRITE_CONFIG_REG));
  425. return 0;
  426. }
  427. static const struct orion_spi_dev orion_spi_dev_data = {
  428. .typ = ORION_SPI,
  429. .min_divisor = 4,
  430. .max_divisor = 30,
  431. .prescale_mask = ORION_SPI_CLK_PRESCALE_MASK,
  432. };
  433. static const struct orion_spi_dev armada_370_spi_dev_data = {
  434. .typ = ARMADA_SPI,
  435. .min_divisor = 4,
  436. .max_divisor = 1920,
  437. .max_hz = 50000000,
  438. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  439. };
  440. static const struct orion_spi_dev armada_xp_spi_dev_data = {
  441. .typ = ARMADA_SPI,
  442. .max_hz = 50000000,
  443. .max_divisor = 1920,
  444. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  445. };
  446. static const struct orion_spi_dev armada_375_spi_dev_data = {
  447. .typ = ARMADA_SPI,
  448. .min_divisor = 15,
  449. .max_divisor = 1920,
  450. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  451. };
  452. static const struct orion_spi_dev armada_380_spi_dev_data = {
  453. .typ = ARMADA_SPI,
  454. .max_hz = 50000000,
  455. .max_divisor = 1920,
  456. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  457. .is_errata_50mhz_ac = true,
  458. };
  459. static const struct of_device_id orion_spi_of_match_table[] = {
  460. {
  461. .compatible = "marvell,orion-spi",
  462. .data = &orion_spi_dev_data,
  463. },
  464. {
  465. .compatible = "marvell,armada-370-spi",
  466. .data = &armada_370_spi_dev_data,
  467. },
  468. {
  469. .compatible = "marvell,armada-375-spi",
  470. .data = &armada_375_spi_dev_data,
  471. },
  472. {
  473. .compatible = "marvell,armada-380-spi",
  474. .data = &armada_380_spi_dev_data,
  475. },
  476. {
  477. .compatible = "marvell,armada-390-spi",
  478. .data = &armada_xp_spi_dev_data,
  479. },
  480. {
  481. .compatible = "marvell,armada-xp-spi",
  482. .data = &armada_xp_spi_dev_data,
  483. },
  484. {}
  485. };
  486. MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
  487. static int orion_spi_probe(struct platform_device *pdev)
  488. {
  489. const struct of_device_id *of_id;
  490. const struct orion_spi_dev *devdata;
  491. struct spi_master *master;
  492. struct orion_spi *spi;
  493. struct resource *r;
  494. unsigned long tclk_hz;
  495. int status = 0;
  496. struct device_node *np;
  497. master = spi_alloc_master(&pdev->dev, sizeof(*spi));
  498. if (master == NULL) {
  499. dev_dbg(&pdev->dev, "master allocation failed\n");
  500. return -ENOMEM;
  501. }
  502. if (pdev->id != -1)
  503. master->bus_num = pdev->id;
  504. if (pdev->dev.of_node) {
  505. u32 cell_index;
  506. if (!of_property_read_u32(pdev->dev.of_node, "cell-index",
  507. &cell_index))
  508. master->bus_num = cell_index;
  509. }
  510. /* we support all 4 SPI modes and LSB first option */
  511. master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
  512. master->set_cs = orion_spi_set_cs;
  513. master->transfer_one = orion_spi_transfer_one;
  514. master->num_chipselect = ORION_NUM_CHIPSELECTS;
  515. master->setup = orion_spi_setup;
  516. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  517. master->auto_runtime_pm = true;
  518. master->flags = SPI_MASTER_GPIO_SS;
  519. platform_set_drvdata(pdev, master);
  520. spi = spi_master_get_devdata(master);
  521. spi->master = master;
  522. spi->unused_hw_gpio = -1;
  523. of_id = of_match_device(orion_spi_of_match_table, &pdev->dev);
  524. devdata = (of_id) ? of_id->data : &orion_spi_dev_data;
  525. spi->devdata = devdata;
  526. spi->clk = devm_clk_get(&pdev->dev, NULL);
  527. if (IS_ERR(spi->clk)) {
  528. status = PTR_ERR(spi->clk);
  529. goto out;
  530. }
  531. status = clk_prepare_enable(spi->clk);
  532. if (status)
  533. goto out;
  534. /* The following clock is only used by some SoCs */
  535. spi->axi_clk = devm_clk_get(&pdev->dev, "axi");
  536. if (IS_ERR(spi->axi_clk) &&
  537. PTR_ERR(spi->axi_clk) == -EPROBE_DEFER) {
  538. status = -EPROBE_DEFER;
  539. goto out_rel_clk;
  540. }
  541. if (!IS_ERR(spi->axi_clk))
  542. clk_prepare_enable(spi->axi_clk);
  543. tclk_hz = clk_get_rate(spi->clk);
  544. /*
  545. * With old device tree, armada-370-spi could be used with
  546. * Armada XP, however for this SoC the maximum frequency is
  547. * 50MHz instead of tclk/4. On Armada 370, tclk cannot be
  548. * higher than 200MHz. So, in order to be able to handle both
  549. * SoCs, we can take the minimum of 50MHz and tclk/4.
  550. */
  551. if (of_device_is_compatible(pdev->dev.of_node,
  552. "marvell,armada-370-spi"))
  553. master->max_speed_hz = min(devdata->max_hz,
  554. DIV_ROUND_UP(tclk_hz, devdata->min_divisor));
  555. else if (devdata->min_divisor)
  556. master->max_speed_hz =
  557. DIV_ROUND_UP(tclk_hz, devdata->min_divisor);
  558. else
  559. master->max_speed_hz = devdata->max_hz;
  560. master->min_speed_hz = DIV_ROUND_UP(tclk_hz, devdata->max_divisor);
  561. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  562. spi->base = devm_ioremap_resource(&pdev->dev, r);
  563. if (IS_ERR(spi->base)) {
  564. status = PTR_ERR(spi->base);
  565. goto out_rel_axi_clk;
  566. }
  567. for_each_available_child_of_node(pdev->dev.of_node, np) {
  568. u32 cs;
  569. int cs_gpio;
  570. /* Get chip-select number from the "reg" property */
  571. status = of_property_read_u32(np, "reg", &cs);
  572. if (status) {
  573. dev_err(&pdev->dev,
  574. "%pOF has no valid 'reg' property (%d)\n",
  575. np, status);
  576. continue;
  577. }
  578. /*
  579. * Initialize the CS GPIO:
  580. * - properly request the actual GPIO signal
  581. * - de-assert the logical signal so that all GPIO CS lines
  582. * are inactive when probing for slaves
  583. * - find an unused physical CS which will be driven for any
  584. * slave which uses a CS GPIO
  585. */
  586. cs_gpio = of_get_named_gpio(pdev->dev.of_node, "cs-gpios", cs);
  587. if (cs_gpio > 0) {
  588. char *gpio_name;
  589. int cs_flags;
  590. if (spi->unused_hw_gpio == -1) {
  591. dev_info(&pdev->dev,
  592. "Selected unused HW CS#%d for any GPIO CSes\n",
  593. cs);
  594. spi->unused_hw_gpio = cs;
  595. }
  596. gpio_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  597. "%s-CS%d", dev_name(&pdev->dev), cs);
  598. if (!gpio_name) {
  599. status = -ENOMEM;
  600. goto out_rel_axi_clk;
  601. }
  602. cs_flags = of_property_read_bool(np, "spi-cs-high") ?
  603. GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH;
  604. status = devm_gpio_request_one(&pdev->dev, cs_gpio,
  605. cs_flags, gpio_name);
  606. if (status) {
  607. dev_err(&pdev->dev,
  608. "Can't request GPIO for CS %d\n", cs);
  609. goto out_rel_axi_clk;
  610. }
  611. }
  612. /*
  613. * Check if an address is configured for this SPI device. If
  614. * not, the MBus mapping via the 'ranges' property in the 'soc'
  615. * node is not configured and this device should not use the
  616. * direct mode. In this case, just continue with the next
  617. * device.
  618. */
  619. status = of_address_to_resource(pdev->dev.of_node, cs + 1, r);
  620. if (status)
  621. continue;
  622. /*
  623. * Only map one page for direct access. This is enough for the
  624. * simple TX transfer which only writes to the first word.
  625. * This needs to get extended for the direct SPI-NOR / SPI-NAND
  626. * support, once this gets implemented.
  627. */
  628. spi->child[cs].direct_access.vaddr = devm_ioremap(&pdev->dev,
  629. r->start,
  630. PAGE_SIZE);
  631. if (!spi->child[cs].direct_access.vaddr) {
  632. status = -ENOMEM;
  633. goto out_rel_axi_clk;
  634. }
  635. spi->child[cs].direct_access.size = PAGE_SIZE;
  636. dev_info(&pdev->dev, "CS%d configured for direct access\n", cs);
  637. }
  638. pm_runtime_set_active(&pdev->dev);
  639. pm_runtime_use_autosuspend(&pdev->dev);
  640. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  641. pm_runtime_enable(&pdev->dev);
  642. status = orion_spi_reset(spi);
  643. if (status < 0)
  644. goto out_rel_pm;
  645. pm_runtime_mark_last_busy(&pdev->dev);
  646. pm_runtime_put_autosuspend(&pdev->dev);
  647. master->dev.of_node = pdev->dev.of_node;
  648. status = spi_register_master(master);
  649. if (status < 0)
  650. goto out_rel_pm;
  651. return status;
  652. out_rel_pm:
  653. pm_runtime_disable(&pdev->dev);
  654. out_rel_axi_clk:
  655. clk_disable_unprepare(spi->axi_clk);
  656. out_rel_clk:
  657. clk_disable_unprepare(spi->clk);
  658. out:
  659. spi_master_put(master);
  660. return status;
  661. }
  662. static int orion_spi_remove(struct platform_device *pdev)
  663. {
  664. struct spi_master *master = platform_get_drvdata(pdev);
  665. struct orion_spi *spi = spi_master_get_devdata(master);
  666. pm_runtime_get_sync(&pdev->dev);
  667. clk_disable_unprepare(spi->axi_clk);
  668. clk_disable_unprepare(spi->clk);
  669. spi_unregister_master(master);
  670. pm_runtime_disable(&pdev->dev);
  671. return 0;
  672. }
  673. MODULE_ALIAS("platform:" DRIVER_NAME);
  674. #ifdef CONFIG_PM
  675. static int orion_spi_runtime_suspend(struct device *dev)
  676. {
  677. struct spi_master *master = dev_get_drvdata(dev);
  678. struct orion_spi *spi = spi_master_get_devdata(master);
  679. clk_disable_unprepare(spi->axi_clk);
  680. clk_disable_unprepare(spi->clk);
  681. return 0;
  682. }
  683. static int orion_spi_runtime_resume(struct device *dev)
  684. {
  685. struct spi_master *master = dev_get_drvdata(dev);
  686. struct orion_spi *spi = spi_master_get_devdata(master);
  687. if (!IS_ERR(spi->axi_clk))
  688. clk_prepare_enable(spi->axi_clk);
  689. return clk_prepare_enable(spi->clk);
  690. }
  691. #endif
  692. static const struct dev_pm_ops orion_spi_pm_ops = {
  693. SET_RUNTIME_PM_OPS(orion_spi_runtime_suspend,
  694. orion_spi_runtime_resume,
  695. NULL)
  696. };
  697. static struct platform_driver orion_spi_driver = {
  698. .driver = {
  699. .name = DRIVER_NAME,
  700. .pm = &orion_spi_pm_ops,
  701. .of_match_table = of_match_ptr(orion_spi_of_match_table),
  702. },
  703. .probe = orion_spi_probe,
  704. .remove = orion_spi_remove,
  705. };
  706. module_platform_driver(orion_spi_driver);
  707. MODULE_DESCRIPTION("Orion SPI driver");
  708. MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
  709. MODULE_LICENSE("GPL");