spi-tegra114.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /*
  2. * SPI driver for NVIDIA's Tegra114 SPI Controller.
  3. *
  4. * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/dmapool.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/kernel.h>
  28. #include <linux/kthread.h>
  29. #include <linux/module.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/reset.h>
  35. #include <linux/spi/spi.h>
  36. #define SPI_COMMAND1 0x000
  37. #define SPI_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  38. #define SPI_PACKED (1 << 5)
  39. #define SPI_TX_EN (1 << 11)
  40. #define SPI_RX_EN (1 << 12)
  41. #define SPI_BOTH_EN_BYTE (1 << 13)
  42. #define SPI_BOTH_EN_BIT (1 << 14)
  43. #define SPI_LSBYTE_FE (1 << 15)
  44. #define SPI_LSBIT_FE (1 << 16)
  45. #define SPI_BIDIROE (1 << 17)
  46. #define SPI_IDLE_SDA_DRIVE_LOW (0 << 18)
  47. #define SPI_IDLE_SDA_DRIVE_HIGH (1 << 18)
  48. #define SPI_IDLE_SDA_PULL_LOW (2 << 18)
  49. #define SPI_IDLE_SDA_PULL_HIGH (3 << 18)
  50. #define SPI_IDLE_SDA_MASK (3 << 18)
  51. #define SPI_CS_SW_VAL (1 << 20)
  52. #define SPI_CS_SW_HW (1 << 21)
  53. /* SPI_CS_POL_INACTIVE bits are default high */
  54. /* n from 0 to 3 */
  55. #define SPI_CS_POL_INACTIVE(n) (1 << (22 + (n)))
  56. #define SPI_CS_POL_INACTIVE_MASK (0xF << 22)
  57. #define SPI_CS_SEL_0 (0 << 26)
  58. #define SPI_CS_SEL_1 (1 << 26)
  59. #define SPI_CS_SEL_2 (2 << 26)
  60. #define SPI_CS_SEL_3 (3 << 26)
  61. #define SPI_CS_SEL_MASK (3 << 26)
  62. #define SPI_CS_SEL(x) (((x) & 0x3) << 26)
  63. #define SPI_CONTROL_MODE_0 (0 << 28)
  64. #define SPI_CONTROL_MODE_1 (1 << 28)
  65. #define SPI_CONTROL_MODE_2 (2 << 28)
  66. #define SPI_CONTROL_MODE_3 (3 << 28)
  67. #define SPI_CONTROL_MODE_MASK (3 << 28)
  68. #define SPI_MODE_SEL(x) (((x) & 0x3) << 28)
  69. #define SPI_M_S (1 << 30)
  70. #define SPI_PIO (1 << 31)
  71. #define SPI_COMMAND2 0x004
  72. #define SPI_TX_TAP_DELAY(x) (((x) & 0x3F) << 6)
  73. #define SPI_RX_TAP_DELAY(x) (((x) & 0x3F) << 0)
  74. #define SPI_CS_TIMING1 0x008
  75. #define SPI_SETUP_HOLD(setup, hold) (((setup) << 4) | (hold))
  76. #define SPI_CS_SETUP_HOLD(reg, cs, val) \
  77. ((((val) & 0xFFu) << ((cs) * 8)) | \
  78. ((reg) & ~(0xFFu << ((cs) * 8))))
  79. #define SPI_CS_TIMING2 0x00C
  80. #define CYCLES_BETWEEN_PACKETS_0(x) (((x) & 0x1F) << 0)
  81. #define CS_ACTIVE_BETWEEN_PACKETS_0 (1 << 5)
  82. #define CYCLES_BETWEEN_PACKETS_1(x) (((x) & 0x1F) << 8)
  83. #define CS_ACTIVE_BETWEEN_PACKETS_1 (1 << 13)
  84. #define CYCLES_BETWEEN_PACKETS_2(x) (((x) & 0x1F) << 16)
  85. #define CS_ACTIVE_BETWEEN_PACKETS_2 (1 << 21)
  86. #define CYCLES_BETWEEN_PACKETS_3(x) (((x) & 0x1F) << 24)
  87. #define CS_ACTIVE_BETWEEN_PACKETS_3 (1 << 29)
  88. #define SPI_SET_CS_ACTIVE_BETWEEN_PACKETS(reg, cs, val) \
  89. (reg = (((val) & 0x1) << ((cs) * 8 + 5)) | \
  90. ((reg) & ~(1 << ((cs) * 8 + 5))))
  91. #define SPI_SET_CYCLES_BETWEEN_PACKETS(reg, cs, val) \
  92. (reg = (((val) & 0xF) << ((cs) * 8)) | \
  93. ((reg) & ~(0xF << ((cs) * 8))))
  94. #define SPI_TRANS_STATUS 0x010
  95. #define SPI_BLK_CNT(val) (((val) >> 0) & 0xFFFF)
  96. #define SPI_SLV_IDLE_COUNT(val) (((val) >> 16) & 0xFF)
  97. #define SPI_RDY (1 << 30)
  98. #define SPI_FIFO_STATUS 0x014
  99. #define SPI_RX_FIFO_EMPTY (1 << 0)
  100. #define SPI_RX_FIFO_FULL (1 << 1)
  101. #define SPI_TX_FIFO_EMPTY (1 << 2)
  102. #define SPI_TX_FIFO_FULL (1 << 3)
  103. #define SPI_RX_FIFO_UNF (1 << 4)
  104. #define SPI_RX_FIFO_OVF (1 << 5)
  105. #define SPI_TX_FIFO_UNF (1 << 6)
  106. #define SPI_TX_FIFO_OVF (1 << 7)
  107. #define SPI_ERR (1 << 8)
  108. #define SPI_TX_FIFO_FLUSH (1 << 14)
  109. #define SPI_RX_FIFO_FLUSH (1 << 15)
  110. #define SPI_TX_FIFO_EMPTY_COUNT(val) (((val) >> 16) & 0x7F)
  111. #define SPI_RX_FIFO_FULL_COUNT(val) (((val) >> 23) & 0x7F)
  112. #define SPI_FRAME_END (1 << 30)
  113. #define SPI_CS_INACTIVE (1 << 31)
  114. #define SPI_FIFO_ERROR (SPI_RX_FIFO_UNF | \
  115. SPI_RX_FIFO_OVF | SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF)
  116. #define SPI_FIFO_EMPTY (SPI_RX_FIFO_EMPTY | SPI_TX_FIFO_EMPTY)
  117. #define SPI_TX_DATA 0x018
  118. #define SPI_RX_DATA 0x01C
  119. #define SPI_DMA_CTL 0x020
  120. #define SPI_TX_TRIG_1 (0 << 15)
  121. #define SPI_TX_TRIG_4 (1 << 15)
  122. #define SPI_TX_TRIG_8 (2 << 15)
  123. #define SPI_TX_TRIG_16 (3 << 15)
  124. #define SPI_TX_TRIG_MASK (3 << 15)
  125. #define SPI_RX_TRIG_1 (0 << 19)
  126. #define SPI_RX_TRIG_4 (1 << 19)
  127. #define SPI_RX_TRIG_8 (2 << 19)
  128. #define SPI_RX_TRIG_16 (3 << 19)
  129. #define SPI_RX_TRIG_MASK (3 << 19)
  130. #define SPI_IE_TX (1 << 28)
  131. #define SPI_IE_RX (1 << 29)
  132. #define SPI_CONT (1 << 30)
  133. #define SPI_DMA (1 << 31)
  134. #define SPI_DMA_EN SPI_DMA
  135. #define SPI_DMA_BLK 0x024
  136. #define SPI_DMA_BLK_SET(x) (((x) & 0xFFFF) << 0)
  137. #define SPI_TX_FIFO 0x108
  138. #define SPI_RX_FIFO 0x188
  139. #define MAX_CHIP_SELECT 4
  140. #define SPI_FIFO_DEPTH 64
  141. #define DATA_DIR_TX (1 << 0)
  142. #define DATA_DIR_RX (1 << 1)
  143. #define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
  144. #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
  145. #define TX_FIFO_EMPTY_COUNT_MAX SPI_TX_FIFO_EMPTY_COUNT(0x40)
  146. #define RX_FIFO_FULL_COUNT_ZERO SPI_RX_FIFO_FULL_COUNT(0)
  147. #define MAX_HOLD_CYCLES 16
  148. #define SPI_DEFAULT_SPEED 25000000
  149. struct tegra_spi_data {
  150. struct device *dev;
  151. struct spi_master *master;
  152. spinlock_t lock;
  153. struct clk *clk;
  154. struct reset_control *rst;
  155. void __iomem *base;
  156. phys_addr_t phys;
  157. unsigned irq;
  158. u32 cur_speed;
  159. struct spi_device *cur_spi;
  160. struct spi_device *cs_control;
  161. unsigned cur_pos;
  162. unsigned words_per_32bit;
  163. unsigned bytes_per_word;
  164. unsigned curr_dma_words;
  165. unsigned cur_direction;
  166. unsigned cur_rx_pos;
  167. unsigned cur_tx_pos;
  168. unsigned dma_buf_size;
  169. unsigned max_buf_size;
  170. bool is_curr_dma_xfer;
  171. struct completion rx_dma_complete;
  172. struct completion tx_dma_complete;
  173. u32 tx_status;
  174. u32 rx_status;
  175. u32 status_reg;
  176. bool is_packed;
  177. u32 command1_reg;
  178. u32 dma_control_reg;
  179. u32 def_command1_reg;
  180. struct completion xfer_completion;
  181. struct spi_transfer *curr_xfer;
  182. struct dma_chan *rx_dma_chan;
  183. u32 *rx_dma_buf;
  184. dma_addr_t rx_dma_phys;
  185. struct dma_async_tx_descriptor *rx_dma_desc;
  186. struct dma_chan *tx_dma_chan;
  187. u32 *tx_dma_buf;
  188. dma_addr_t tx_dma_phys;
  189. struct dma_async_tx_descriptor *tx_dma_desc;
  190. };
  191. static int tegra_spi_runtime_suspend(struct device *dev);
  192. static int tegra_spi_runtime_resume(struct device *dev);
  193. static inline u32 tegra_spi_readl(struct tegra_spi_data *tspi,
  194. unsigned long reg)
  195. {
  196. return readl(tspi->base + reg);
  197. }
  198. static inline void tegra_spi_writel(struct tegra_spi_data *tspi,
  199. u32 val, unsigned long reg)
  200. {
  201. writel(val, tspi->base + reg);
  202. /* Read back register to make sure that register writes completed */
  203. if (reg != SPI_TX_FIFO)
  204. readl(tspi->base + SPI_COMMAND1);
  205. }
  206. static void tegra_spi_clear_status(struct tegra_spi_data *tspi)
  207. {
  208. u32 val;
  209. /* Write 1 to clear status register */
  210. val = tegra_spi_readl(tspi, SPI_TRANS_STATUS);
  211. tegra_spi_writel(tspi, val, SPI_TRANS_STATUS);
  212. /* Clear fifo status error if any */
  213. val = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  214. if (val & SPI_ERR)
  215. tegra_spi_writel(tspi, SPI_ERR | SPI_FIFO_ERROR,
  216. SPI_FIFO_STATUS);
  217. }
  218. static unsigned tegra_spi_calculate_curr_xfer_param(
  219. struct spi_device *spi, struct tegra_spi_data *tspi,
  220. struct spi_transfer *t)
  221. {
  222. unsigned remain_len = t->len - tspi->cur_pos;
  223. unsigned max_word;
  224. unsigned bits_per_word = t->bits_per_word;
  225. unsigned max_len;
  226. unsigned total_fifo_words;
  227. tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
  228. if (bits_per_word == 8 || bits_per_word == 16) {
  229. tspi->is_packed = 1;
  230. tspi->words_per_32bit = 32/bits_per_word;
  231. } else {
  232. tspi->is_packed = 0;
  233. tspi->words_per_32bit = 1;
  234. }
  235. if (tspi->is_packed) {
  236. max_len = min(remain_len, tspi->max_buf_size);
  237. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  238. total_fifo_words = (max_len + 3) / 4;
  239. } else {
  240. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  241. max_word = min(max_word, tspi->max_buf_size/4);
  242. tspi->curr_dma_words = max_word;
  243. total_fifo_words = max_word;
  244. }
  245. return total_fifo_words;
  246. }
  247. static unsigned tegra_spi_fill_tx_fifo_from_client_txbuf(
  248. struct tegra_spi_data *tspi, struct spi_transfer *t)
  249. {
  250. unsigned nbytes;
  251. unsigned tx_empty_count;
  252. u32 fifo_status;
  253. unsigned max_n_32bit;
  254. unsigned i, count;
  255. unsigned int written_words;
  256. unsigned fifo_words_left;
  257. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  258. fifo_status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  259. tx_empty_count = SPI_TX_FIFO_EMPTY_COUNT(fifo_status);
  260. if (tspi->is_packed) {
  261. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  262. written_words = min(fifo_words_left, tspi->curr_dma_words);
  263. nbytes = written_words * tspi->bytes_per_word;
  264. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  265. for (count = 0; count < max_n_32bit; count++) {
  266. u32 x = 0;
  267. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  268. x |= (u32)(*tx_buf++) << (i * 8);
  269. tegra_spi_writel(tspi, x, SPI_TX_FIFO);
  270. }
  271. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  272. } else {
  273. unsigned int write_bytes;
  274. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  275. written_words = max_n_32bit;
  276. nbytes = written_words * tspi->bytes_per_word;
  277. if (nbytes > t->len - tspi->cur_pos)
  278. nbytes = t->len - tspi->cur_pos;
  279. write_bytes = nbytes;
  280. for (count = 0; count < max_n_32bit; count++) {
  281. u32 x = 0;
  282. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  283. i++, nbytes--)
  284. x |= (u32)(*tx_buf++) << (i * 8);
  285. tegra_spi_writel(tspi, x, SPI_TX_FIFO);
  286. }
  287. tspi->cur_tx_pos += write_bytes;
  288. }
  289. return written_words;
  290. }
  291. static unsigned int tegra_spi_read_rx_fifo_to_client_rxbuf(
  292. struct tegra_spi_data *tspi, struct spi_transfer *t)
  293. {
  294. unsigned rx_full_count;
  295. u32 fifo_status;
  296. unsigned i, count;
  297. unsigned int read_words = 0;
  298. unsigned len;
  299. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  300. fifo_status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  301. rx_full_count = SPI_RX_FIFO_FULL_COUNT(fifo_status);
  302. if (tspi->is_packed) {
  303. len = tspi->curr_dma_words * tspi->bytes_per_word;
  304. for (count = 0; count < rx_full_count; count++) {
  305. u32 x = tegra_spi_readl(tspi, SPI_RX_FIFO);
  306. for (i = 0; len && (i < 4); i++, len--)
  307. *rx_buf++ = (x >> i*8) & 0xFF;
  308. }
  309. read_words += tspi->curr_dma_words;
  310. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  311. } else {
  312. u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
  313. u8 bytes_per_word = tspi->bytes_per_word;
  314. unsigned int read_bytes;
  315. len = rx_full_count * bytes_per_word;
  316. if (len > t->len - tspi->cur_pos)
  317. len = t->len - tspi->cur_pos;
  318. read_bytes = len;
  319. for (count = 0; count < rx_full_count; count++) {
  320. u32 x = tegra_spi_readl(tspi, SPI_RX_FIFO) & rx_mask;
  321. for (i = 0; len && (i < bytes_per_word); i++, len--)
  322. *rx_buf++ = (x >> (i*8)) & 0xFF;
  323. }
  324. read_words += rx_full_count;
  325. tspi->cur_rx_pos += read_bytes;
  326. }
  327. return read_words;
  328. }
  329. static void tegra_spi_copy_client_txbuf_to_spi_txbuf(
  330. struct tegra_spi_data *tspi, struct spi_transfer *t)
  331. {
  332. /* Make the dma buffer to read by cpu */
  333. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  334. tspi->dma_buf_size, DMA_TO_DEVICE);
  335. if (tspi->is_packed) {
  336. unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
  337. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  338. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  339. } else {
  340. unsigned int i;
  341. unsigned int count;
  342. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  343. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  344. unsigned int write_bytes;
  345. if (consume > t->len - tspi->cur_pos)
  346. consume = t->len - tspi->cur_pos;
  347. write_bytes = consume;
  348. for (count = 0; count < tspi->curr_dma_words; count++) {
  349. u32 x = 0;
  350. for (i = 0; consume && (i < tspi->bytes_per_word);
  351. i++, consume--)
  352. x |= (u32)(*tx_buf++) << (i * 8);
  353. tspi->tx_dma_buf[count] = x;
  354. }
  355. tspi->cur_tx_pos += write_bytes;
  356. }
  357. /* Make the dma buffer to read by dma */
  358. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  359. tspi->dma_buf_size, DMA_TO_DEVICE);
  360. }
  361. static void tegra_spi_copy_spi_rxbuf_to_client_rxbuf(
  362. struct tegra_spi_data *tspi, struct spi_transfer *t)
  363. {
  364. /* Make the dma buffer to read by cpu */
  365. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  366. tspi->dma_buf_size, DMA_FROM_DEVICE);
  367. if (tspi->is_packed) {
  368. unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
  369. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  370. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  371. } else {
  372. unsigned int i;
  373. unsigned int count;
  374. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  375. u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
  376. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  377. unsigned int read_bytes;
  378. if (consume > t->len - tspi->cur_pos)
  379. consume = t->len - tspi->cur_pos;
  380. read_bytes = consume;
  381. for (count = 0; count < tspi->curr_dma_words; count++) {
  382. u32 x = tspi->rx_dma_buf[count] & rx_mask;
  383. for (i = 0; consume && (i < tspi->bytes_per_word);
  384. i++, consume--)
  385. *rx_buf++ = (x >> (i*8)) & 0xFF;
  386. }
  387. tspi->cur_rx_pos += read_bytes;
  388. }
  389. /* Make the dma buffer to read by dma */
  390. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  391. tspi->dma_buf_size, DMA_FROM_DEVICE);
  392. }
  393. static void tegra_spi_dma_complete(void *args)
  394. {
  395. struct completion *dma_complete = args;
  396. complete(dma_complete);
  397. }
  398. static int tegra_spi_start_tx_dma(struct tegra_spi_data *tspi, int len)
  399. {
  400. reinit_completion(&tspi->tx_dma_complete);
  401. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  402. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  403. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  404. if (!tspi->tx_dma_desc) {
  405. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  406. return -EIO;
  407. }
  408. tspi->tx_dma_desc->callback = tegra_spi_dma_complete;
  409. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  410. dmaengine_submit(tspi->tx_dma_desc);
  411. dma_async_issue_pending(tspi->tx_dma_chan);
  412. return 0;
  413. }
  414. static int tegra_spi_start_rx_dma(struct tegra_spi_data *tspi, int len)
  415. {
  416. reinit_completion(&tspi->rx_dma_complete);
  417. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  418. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  419. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  420. if (!tspi->rx_dma_desc) {
  421. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  422. return -EIO;
  423. }
  424. tspi->rx_dma_desc->callback = tegra_spi_dma_complete;
  425. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  426. dmaengine_submit(tspi->rx_dma_desc);
  427. dma_async_issue_pending(tspi->rx_dma_chan);
  428. return 0;
  429. }
  430. static int tegra_spi_flush_fifos(struct tegra_spi_data *tspi)
  431. {
  432. unsigned long timeout = jiffies + HZ;
  433. u32 status;
  434. status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  435. if ((status & SPI_FIFO_EMPTY) != SPI_FIFO_EMPTY) {
  436. status |= SPI_RX_FIFO_FLUSH | SPI_TX_FIFO_FLUSH;
  437. tegra_spi_writel(tspi, status, SPI_FIFO_STATUS);
  438. while ((status & SPI_FIFO_EMPTY) != SPI_FIFO_EMPTY) {
  439. status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  440. if (time_after(jiffies, timeout)) {
  441. dev_err(tspi->dev,
  442. "timeout waiting for fifo flush\n");
  443. return -EIO;
  444. }
  445. udelay(1);
  446. }
  447. }
  448. return 0;
  449. }
  450. static int tegra_spi_start_dma_based_transfer(
  451. struct tegra_spi_data *tspi, struct spi_transfer *t)
  452. {
  453. u32 val;
  454. unsigned int len;
  455. int ret = 0;
  456. u8 dma_burst;
  457. struct dma_slave_config dma_sconfig = {0};
  458. val = SPI_DMA_BLK_SET(tspi->curr_dma_words - 1);
  459. tegra_spi_writel(tspi, val, SPI_DMA_BLK);
  460. if (tspi->is_packed)
  461. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  462. 4) * 4;
  463. else
  464. len = tspi->curr_dma_words * 4;
  465. /* Set attention level based on length of transfer */
  466. if (len & 0xF) {
  467. val |= SPI_TX_TRIG_1 | SPI_RX_TRIG_1;
  468. dma_burst = 1;
  469. } else if (((len) >> 4) & 0x1) {
  470. val |= SPI_TX_TRIG_4 | SPI_RX_TRIG_4;
  471. dma_burst = 4;
  472. } else {
  473. val |= SPI_TX_TRIG_8 | SPI_RX_TRIG_8;
  474. dma_burst = 8;
  475. }
  476. if (tspi->cur_direction & DATA_DIR_TX)
  477. val |= SPI_IE_TX;
  478. if (tspi->cur_direction & DATA_DIR_RX)
  479. val |= SPI_IE_RX;
  480. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  481. tspi->dma_control_reg = val;
  482. dma_sconfig.device_fc = true;
  483. if (tspi->cur_direction & DATA_DIR_TX) {
  484. dma_sconfig.dst_addr = tspi->phys + SPI_TX_FIFO;
  485. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  486. dma_sconfig.dst_maxburst = dma_burst;
  487. ret = dmaengine_slave_config(tspi->tx_dma_chan, &dma_sconfig);
  488. if (ret < 0) {
  489. dev_err(tspi->dev,
  490. "DMA slave config failed: %d\n", ret);
  491. return ret;
  492. }
  493. tegra_spi_copy_client_txbuf_to_spi_txbuf(tspi, t);
  494. ret = tegra_spi_start_tx_dma(tspi, len);
  495. if (ret < 0) {
  496. dev_err(tspi->dev,
  497. "Starting tx dma failed, err %d\n", ret);
  498. return ret;
  499. }
  500. }
  501. if (tspi->cur_direction & DATA_DIR_RX) {
  502. dma_sconfig.src_addr = tspi->phys + SPI_RX_FIFO;
  503. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  504. dma_sconfig.src_maxburst = dma_burst;
  505. ret = dmaengine_slave_config(tspi->rx_dma_chan, &dma_sconfig);
  506. if (ret < 0) {
  507. dev_err(tspi->dev,
  508. "DMA slave config failed: %d\n", ret);
  509. return ret;
  510. }
  511. /* Make the dma buffer to read by dma */
  512. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  513. tspi->dma_buf_size, DMA_FROM_DEVICE);
  514. ret = tegra_spi_start_rx_dma(tspi, len);
  515. if (ret < 0) {
  516. dev_err(tspi->dev,
  517. "Starting rx dma failed, err %d\n", ret);
  518. if (tspi->cur_direction & DATA_DIR_TX)
  519. dmaengine_terminate_all(tspi->tx_dma_chan);
  520. return ret;
  521. }
  522. }
  523. tspi->is_curr_dma_xfer = true;
  524. tspi->dma_control_reg = val;
  525. val |= SPI_DMA_EN;
  526. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  527. return ret;
  528. }
  529. static int tegra_spi_start_cpu_based_transfer(
  530. struct tegra_spi_data *tspi, struct spi_transfer *t)
  531. {
  532. u32 val;
  533. unsigned cur_words;
  534. if (tspi->cur_direction & DATA_DIR_TX)
  535. cur_words = tegra_spi_fill_tx_fifo_from_client_txbuf(tspi, t);
  536. else
  537. cur_words = tspi->curr_dma_words;
  538. val = SPI_DMA_BLK_SET(cur_words - 1);
  539. tegra_spi_writel(tspi, val, SPI_DMA_BLK);
  540. val = 0;
  541. if (tspi->cur_direction & DATA_DIR_TX)
  542. val |= SPI_IE_TX;
  543. if (tspi->cur_direction & DATA_DIR_RX)
  544. val |= SPI_IE_RX;
  545. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  546. tspi->dma_control_reg = val;
  547. tspi->is_curr_dma_xfer = false;
  548. val |= SPI_DMA_EN;
  549. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  550. return 0;
  551. }
  552. static int tegra_spi_init_dma_param(struct tegra_spi_data *tspi,
  553. bool dma_to_memory)
  554. {
  555. struct dma_chan *dma_chan;
  556. u32 *dma_buf;
  557. dma_addr_t dma_phys;
  558. int ret;
  559. dma_chan = dma_request_slave_channel_reason(tspi->dev,
  560. dma_to_memory ? "rx" : "tx");
  561. if (IS_ERR(dma_chan)) {
  562. ret = PTR_ERR(dma_chan);
  563. if (ret != -EPROBE_DEFER)
  564. dev_err(tspi->dev,
  565. "Dma channel is not available: %d\n", ret);
  566. return ret;
  567. }
  568. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  569. &dma_phys, GFP_KERNEL);
  570. if (!dma_buf) {
  571. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  572. dma_release_channel(dma_chan);
  573. return -ENOMEM;
  574. }
  575. if (dma_to_memory) {
  576. tspi->rx_dma_chan = dma_chan;
  577. tspi->rx_dma_buf = dma_buf;
  578. tspi->rx_dma_phys = dma_phys;
  579. } else {
  580. tspi->tx_dma_chan = dma_chan;
  581. tspi->tx_dma_buf = dma_buf;
  582. tspi->tx_dma_phys = dma_phys;
  583. }
  584. return 0;
  585. }
  586. static void tegra_spi_deinit_dma_param(struct tegra_spi_data *tspi,
  587. bool dma_to_memory)
  588. {
  589. u32 *dma_buf;
  590. dma_addr_t dma_phys;
  591. struct dma_chan *dma_chan;
  592. if (dma_to_memory) {
  593. dma_buf = tspi->rx_dma_buf;
  594. dma_chan = tspi->rx_dma_chan;
  595. dma_phys = tspi->rx_dma_phys;
  596. tspi->rx_dma_chan = NULL;
  597. tspi->rx_dma_buf = NULL;
  598. } else {
  599. dma_buf = tspi->tx_dma_buf;
  600. dma_chan = tspi->tx_dma_chan;
  601. dma_phys = tspi->tx_dma_phys;
  602. tspi->tx_dma_buf = NULL;
  603. tspi->tx_dma_chan = NULL;
  604. }
  605. if (!dma_chan)
  606. return;
  607. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  608. dma_release_channel(dma_chan);
  609. }
  610. static u32 tegra_spi_setup_transfer_one(struct spi_device *spi,
  611. struct spi_transfer *t, bool is_first_of_msg)
  612. {
  613. struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
  614. u32 speed = t->speed_hz;
  615. u8 bits_per_word = t->bits_per_word;
  616. u32 command1;
  617. int req_mode;
  618. if (speed != tspi->cur_speed) {
  619. clk_set_rate(tspi->clk, speed);
  620. tspi->cur_speed = speed;
  621. }
  622. tspi->cur_spi = spi;
  623. tspi->cur_pos = 0;
  624. tspi->cur_rx_pos = 0;
  625. tspi->cur_tx_pos = 0;
  626. tspi->curr_xfer = t;
  627. if (is_first_of_msg) {
  628. tegra_spi_clear_status(tspi);
  629. command1 = tspi->def_command1_reg;
  630. command1 |= SPI_BIT_LENGTH(bits_per_word - 1);
  631. command1 &= ~SPI_CONTROL_MODE_MASK;
  632. req_mode = spi->mode & 0x3;
  633. if (req_mode == SPI_MODE_0)
  634. command1 |= SPI_CONTROL_MODE_0;
  635. else if (req_mode == SPI_MODE_1)
  636. command1 |= SPI_CONTROL_MODE_1;
  637. else if (req_mode == SPI_MODE_2)
  638. command1 |= SPI_CONTROL_MODE_2;
  639. else if (req_mode == SPI_MODE_3)
  640. command1 |= SPI_CONTROL_MODE_3;
  641. if (tspi->cs_control) {
  642. if (tspi->cs_control != spi)
  643. tegra_spi_writel(tspi, command1, SPI_COMMAND1);
  644. tspi->cs_control = NULL;
  645. } else
  646. tegra_spi_writel(tspi, command1, SPI_COMMAND1);
  647. command1 |= SPI_CS_SW_HW;
  648. if (spi->mode & SPI_CS_HIGH)
  649. command1 |= SPI_CS_SW_VAL;
  650. else
  651. command1 &= ~SPI_CS_SW_VAL;
  652. tegra_spi_writel(tspi, 0, SPI_COMMAND2);
  653. } else {
  654. command1 = tspi->command1_reg;
  655. command1 &= ~SPI_BIT_LENGTH(~0);
  656. command1 |= SPI_BIT_LENGTH(bits_per_word - 1);
  657. }
  658. return command1;
  659. }
  660. static int tegra_spi_start_transfer_one(struct spi_device *spi,
  661. struct spi_transfer *t, u32 command1)
  662. {
  663. struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
  664. unsigned total_fifo_words;
  665. int ret;
  666. total_fifo_words = tegra_spi_calculate_curr_xfer_param(spi, tspi, t);
  667. if (tspi->is_packed)
  668. command1 |= SPI_PACKED;
  669. else
  670. command1 &= ~SPI_PACKED;
  671. command1 &= ~(SPI_CS_SEL_MASK | SPI_TX_EN | SPI_RX_EN);
  672. tspi->cur_direction = 0;
  673. if (t->rx_buf) {
  674. command1 |= SPI_RX_EN;
  675. tspi->cur_direction |= DATA_DIR_RX;
  676. }
  677. if (t->tx_buf) {
  678. command1 |= SPI_TX_EN;
  679. tspi->cur_direction |= DATA_DIR_TX;
  680. }
  681. command1 |= SPI_CS_SEL(spi->chip_select);
  682. tegra_spi_writel(tspi, command1, SPI_COMMAND1);
  683. tspi->command1_reg = command1;
  684. dev_dbg(tspi->dev, "The def 0x%x and written 0x%x\n",
  685. tspi->def_command1_reg, (unsigned)command1);
  686. ret = tegra_spi_flush_fifos(tspi);
  687. if (ret < 0)
  688. return ret;
  689. if (total_fifo_words > SPI_FIFO_DEPTH)
  690. ret = tegra_spi_start_dma_based_transfer(tspi, t);
  691. else
  692. ret = tegra_spi_start_cpu_based_transfer(tspi, t);
  693. return ret;
  694. }
  695. static int tegra_spi_setup(struct spi_device *spi)
  696. {
  697. struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
  698. u32 val;
  699. unsigned long flags;
  700. int ret;
  701. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  702. spi->bits_per_word,
  703. spi->mode & SPI_CPOL ? "" : "~",
  704. spi->mode & SPI_CPHA ? "" : "~",
  705. spi->max_speed_hz);
  706. ret = pm_runtime_get_sync(tspi->dev);
  707. if (ret < 0) {
  708. pm_runtime_put_noidle(tspi->dev);
  709. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  710. return ret;
  711. }
  712. spin_lock_irqsave(&tspi->lock, flags);
  713. val = tspi->def_command1_reg;
  714. if (spi->mode & SPI_CS_HIGH)
  715. val &= ~SPI_CS_POL_INACTIVE(spi->chip_select);
  716. else
  717. val |= SPI_CS_POL_INACTIVE(spi->chip_select);
  718. tspi->def_command1_reg = val;
  719. tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
  720. spin_unlock_irqrestore(&tspi->lock, flags);
  721. pm_runtime_put(tspi->dev);
  722. return 0;
  723. }
  724. static void tegra_spi_transfer_delay(int delay)
  725. {
  726. if (!delay)
  727. return;
  728. if (delay >= 1000)
  729. mdelay(delay / 1000);
  730. udelay(delay % 1000);
  731. }
  732. static int tegra_spi_transfer_one_message(struct spi_master *master,
  733. struct spi_message *msg)
  734. {
  735. bool is_first_msg = true;
  736. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  737. struct spi_transfer *xfer;
  738. struct spi_device *spi = msg->spi;
  739. int ret;
  740. bool skip = false;
  741. msg->status = 0;
  742. msg->actual_length = 0;
  743. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  744. u32 cmd1;
  745. reinit_completion(&tspi->xfer_completion);
  746. cmd1 = tegra_spi_setup_transfer_one(spi, xfer, is_first_msg);
  747. if (!xfer->len) {
  748. ret = 0;
  749. skip = true;
  750. goto complete_xfer;
  751. }
  752. ret = tegra_spi_start_transfer_one(spi, xfer, cmd1);
  753. if (ret < 0) {
  754. dev_err(tspi->dev,
  755. "spi can not start transfer, err %d\n", ret);
  756. goto complete_xfer;
  757. }
  758. is_first_msg = false;
  759. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  760. SPI_DMA_TIMEOUT);
  761. if (WARN_ON(ret == 0)) {
  762. dev_err(tspi->dev,
  763. "spi transfer timeout, err %d\n", ret);
  764. if (tspi->is_curr_dma_xfer &&
  765. (tspi->cur_direction & DATA_DIR_TX))
  766. dmaengine_terminate_all(tspi->tx_dma_chan);
  767. if (tspi->is_curr_dma_xfer &&
  768. (tspi->cur_direction & DATA_DIR_RX))
  769. dmaengine_terminate_all(tspi->rx_dma_chan);
  770. ret = -EIO;
  771. tegra_spi_flush_fifos(tspi);
  772. reset_control_assert(tspi->rst);
  773. udelay(2);
  774. reset_control_deassert(tspi->rst);
  775. goto complete_xfer;
  776. }
  777. if (tspi->tx_status || tspi->rx_status) {
  778. dev_err(tspi->dev, "Error in Transfer\n");
  779. ret = -EIO;
  780. goto complete_xfer;
  781. }
  782. msg->actual_length += xfer->len;
  783. complete_xfer:
  784. if (ret < 0 || skip) {
  785. tegra_spi_writel(tspi, tspi->def_command1_reg,
  786. SPI_COMMAND1);
  787. tegra_spi_transfer_delay(xfer->delay_usecs);
  788. goto exit;
  789. } else if (list_is_last(&xfer->transfer_list,
  790. &msg->transfers)) {
  791. if (xfer->cs_change)
  792. tspi->cs_control = spi;
  793. else {
  794. tegra_spi_writel(tspi, tspi->def_command1_reg,
  795. SPI_COMMAND1);
  796. tegra_spi_transfer_delay(xfer->delay_usecs);
  797. }
  798. } else if (xfer->cs_change) {
  799. tegra_spi_writel(tspi, tspi->def_command1_reg,
  800. SPI_COMMAND1);
  801. tegra_spi_transfer_delay(xfer->delay_usecs);
  802. }
  803. }
  804. ret = 0;
  805. exit:
  806. msg->status = ret;
  807. spi_finalize_current_message(master);
  808. return ret;
  809. }
  810. static irqreturn_t handle_cpu_based_xfer(struct tegra_spi_data *tspi)
  811. {
  812. struct spi_transfer *t = tspi->curr_xfer;
  813. unsigned long flags;
  814. spin_lock_irqsave(&tspi->lock, flags);
  815. if (tspi->tx_status || tspi->rx_status) {
  816. dev_err(tspi->dev, "CpuXfer ERROR bit set 0x%x\n",
  817. tspi->status_reg);
  818. dev_err(tspi->dev, "CpuXfer 0x%08x:0x%08x\n",
  819. tspi->command1_reg, tspi->dma_control_reg);
  820. tegra_spi_flush_fifos(tspi);
  821. reset_control_assert(tspi->rst);
  822. udelay(2);
  823. reset_control_deassert(tspi->rst);
  824. complete(&tspi->xfer_completion);
  825. goto exit;
  826. }
  827. if (tspi->cur_direction & DATA_DIR_RX)
  828. tegra_spi_read_rx_fifo_to_client_rxbuf(tspi, t);
  829. if (tspi->cur_direction & DATA_DIR_TX)
  830. tspi->cur_pos = tspi->cur_tx_pos;
  831. else
  832. tspi->cur_pos = tspi->cur_rx_pos;
  833. if (tspi->cur_pos == t->len) {
  834. complete(&tspi->xfer_completion);
  835. goto exit;
  836. }
  837. tegra_spi_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  838. tegra_spi_start_cpu_based_transfer(tspi, t);
  839. exit:
  840. spin_unlock_irqrestore(&tspi->lock, flags);
  841. return IRQ_HANDLED;
  842. }
  843. static irqreturn_t handle_dma_based_xfer(struct tegra_spi_data *tspi)
  844. {
  845. struct spi_transfer *t = tspi->curr_xfer;
  846. long wait_status;
  847. int err = 0;
  848. unsigned total_fifo_words;
  849. unsigned long flags;
  850. /* Abort dmas if any error */
  851. if (tspi->cur_direction & DATA_DIR_TX) {
  852. if (tspi->tx_status) {
  853. dmaengine_terminate_all(tspi->tx_dma_chan);
  854. err += 1;
  855. } else {
  856. wait_status = wait_for_completion_interruptible_timeout(
  857. &tspi->tx_dma_complete, SPI_DMA_TIMEOUT);
  858. if (wait_status <= 0) {
  859. dmaengine_terminate_all(tspi->tx_dma_chan);
  860. dev_err(tspi->dev, "TxDma Xfer failed\n");
  861. err += 1;
  862. }
  863. }
  864. }
  865. if (tspi->cur_direction & DATA_DIR_RX) {
  866. if (tspi->rx_status) {
  867. dmaengine_terminate_all(tspi->rx_dma_chan);
  868. err += 2;
  869. } else {
  870. wait_status = wait_for_completion_interruptible_timeout(
  871. &tspi->rx_dma_complete, SPI_DMA_TIMEOUT);
  872. if (wait_status <= 0) {
  873. dmaengine_terminate_all(tspi->rx_dma_chan);
  874. dev_err(tspi->dev, "RxDma Xfer failed\n");
  875. err += 2;
  876. }
  877. }
  878. }
  879. spin_lock_irqsave(&tspi->lock, flags);
  880. if (err) {
  881. dev_err(tspi->dev, "DmaXfer: ERROR bit set 0x%x\n",
  882. tspi->status_reg);
  883. dev_err(tspi->dev, "DmaXfer 0x%08x:0x%08x\n",
  884. tspi->command1_reg, tspi->dma_control_reg);
  885. tegra_spi_flush_fifos(tspi);
  886. reset_control_assert(tspi->rst);
  887. udelay(2);
  888. reset_control_deassert(tspi->rst);
  889. complete(&tspi->xfer_completion);
  890. spin_unlock_irqrestore(&tspi->lock, flags);
  891. return IRQ_HANDLED;
  892. }
  893. if (tspi->cur_direction & DATA_DIR_RX)
  894. tegra_spi_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  895. if (tspi->cur_direction & DATA_DIR_TX)
  896. tspi->cur_pos = tspi->cur_tx_pos;
  897. else
  898. tspi->cur_pos = tspi->cur_rx_pos;
  899. if (tspi->cur_pos == t->len) {
  900. complete(&tspi->xfer_completion);
  901. goto exit;
  902. }
  903. /* Continue transfer in current message */
  904. total_fifo_words = tegra_spi_calculate_curr_xfer_param(tspi->cur_spi,
  905. tspi, t);
  906. if (total_fifo_words > SPI_FIFO_DEPTH)
  907. err = tegra_spi_start_dma_based_transfer(tspi, t);
  908. else
  909. err = tegra_spi_start_cpu_based_transfer(tspi, t);
  910. exit:
  911. spin_unlock_irqrestore(&tspi->lock, flags);
  912. return IRQ_HANDLED;
  913. }
  914. static irqreturn_t tegra_spi_isr_thread(int irq, void *context_data)
  915. {
  916. struct tegra_spi_data *tspi = context_data;
  917. if (!tspi->is_curr_dma_xfer)
  918. return handle_cpu_based_xfer(tspi);
  919. return handle_dma_based_xfer(tspi);
  920. }
  921. static irqreturn_t tegra_spi_isr(int irq, void *context_data)
  922. {
  923. struct tegra_spi_data *tspi = context_data;
  924. tspi->status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  925. if (tspi->cur_direction & DATA_DIR_TX)
  926. tspi->tx_status = tspi->status_reg &
  927. (SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF);
  928. if (tspi->cur_direction & DATA_DIR_RX)
  929. tspi->rx_status = tspi->status_reg &
  930. (SPI_RX_FIFO_OVF | SPI_RX_FIFO_UNF);
  931. tegra_spi_clear_status(tspi);
  932. return IRQ_WAKE_THREAD;
  933. }
  934. static const struct of_device_id tegra_spi_of_match[] = {
  935. { .compatible = "nvidia,tegra114-spi", },
  936. {}
  937. };
  938. MODULE_DEVICE_TABLE(of, tegra_spi_of_match);
  939. static int tegra_spi_probe(struct platform_device *pdev)
  940. {
  941. struct spi_master *master;
  942. struct tegra_spi_data *tspi;
  943. struct resource *r;
  944. int ret, spi_irq;
  945. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  946. if (!master) {
  947. dev_err(&pdev->dev, "master allocation failed\n");
  948. return -ENOMEM;
  949. }
  950. platform_set_drvdata(pdev, master);
  951. tspi = spi_master_get_devdata(master);
  952. if (of_property_read_u32(pdev->dev.of_node, "spi-max-frequency",
  953. &master->max_speed_hz))
  954. master->max_speed_hz = 25000000; /* 25MHz */
  955. /* the spi->mode bits understood by this driver: */
  956. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  957. master->setup = tegra_spi_setup;
  958. master->transfer_one_message = tegra_spi_transfer_one_message;
  959. master->num_chipselect = MAX_CHIP_SELECT;
  960. master->auto_runtime_pm = true;
  961. tspi->master = master;
  962. tspi->dev = &pdev->dev;
  963. spin_lock_init(&tspi->lock);
  964. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  965. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  966. if (IS_ERR(tspi->base)) {
  967. ret = PTR_ERR(tspi->base);
  968. goto exit_free_master;
  969. }
  970. tspi->phys = r->start;
  971. spi_irq = platform_get_irq(pdev, 0);
  972. tspi->irq = spi_irq;
  973. tspi->clk = devm_clk_get(&pdev->dev, "spi");
  974. if (IS_ERR(tspi->clk)) {
  975. dev_err(&pdev->dev, "can not get clock\n");
  976. ret = PTR_ERR(tspi->clk);
  977. goto exit_free_master;
  978. }
  979. tspi->rst = devm_reset_control_get_exclusive(&pdev->dev, "spi");
  980. if (IS_ERR(tspi->rst)) {
  981. dev_err(&pdev->dev, "can not get reset\n");
  982. ret = PTR_ERR(tspi->rst);
  983. goto exit_free_master;
  984. }
  985. tspi->max_buf_size = SPI_FIFO_DEPTH << 2;
  986. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  987. ret = tegra_spi_init_dma_param(tspi, true);
  988. if (ret < 0)
  989. goto exit_free_master;
  990. ret = tegra_spi_init_dma_param(tspi, false);
  991. if (ret < 0)
  992. goto exit_rx_dma_free;
  993. tspi->max_buf_size = tspi->dma_buf_size;
  994. init_completion(&tspi->tx_dma_complete);
  995. init_completion(&tspi->rx_dma_complete);
  996. init_completion(&tspi->xfer_completion);
  997. pm_runtime_enable(&pdev->dev);
  998. if (!pm_runtime_enabled(&pdev->dev)) {
  999. ret = tegra_spi_runtime_resume(&pdev->dev);
  1000. if (ret)
  1001. goto exit_pm_disable;
  1002. }
  1003. ret = pm_runtime_get_sync(&pdev->dev);
  1004. if (ret < 0) {
  1005. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  1006. goto exit_pm_disable;
  1007. }
  1008. reset_control_assert(tspi->rst);
  1009. udelay(2);
  1010. reset_control_deassert(tspi->rst);
  1011. tspi->def_command1_reg = SPI_M_S;
  1012. tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
  1013. pm_runtime_put(&pdev->dev);
  1014. ret = request_threaded_irq(tspi->irq, tegra_spi_isr,
  1015. tegra_spi_isr_thread, IRQF_ONESHOT,
  1016. dev_name(&pdev->dev), tspi);
  1017. if (ret < 0) {
  1018. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  1019. tspi->irq);
  1020. goto exit_pm_disable;
  1021. }
  1022. master->dev.of_node = pdev->dev.of_node;
  1023. ret = devm_spi_register_master(&pdev->dev, master);
  1024. if (ret < 0) {
  1025. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  1026. goto exit_free_irq;
  1027. }
  1028. return ret;
  1029. exit_free_irq:
  1030. free_irq(spi_irq, tspi);
  1031. exit_pm_disable:
  1032. pm_runtime_disable(&pdev->dev);
  1033. if (!pm_runtime_status_suspended(&pdev->dev))
  1034. tegra_spi_runtime_suspend(&pdev->dev);
  1035. tegra_spi_deinit_dma_param(tspi, false);
  1036. exit_rx_dma_free:
  1037. tegra_spi_deinit_dma_param(tspi, true);
  1038. exit_free_master:
  1039. spi_master_put(master);
  1040. return ret;
  1041. }
  1042. static int tegra_spi_remove(struct platform_device *pdev)
  1043. {
  1044. struct spi_master *master = platform_get_drvdata(pdev);
  1045. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1046. free_irq(tspi->irq, tspi);
  1047. if (tspi->tx_dma_chan)
  1048. tegra_spi_deinit_dma_param(tspi, false);
  1049. if (tspi->rx_dma_chan)
  1050. tegra_spi_deinit_dma_param(tspi, true);
  1051. pm_runtime_disable(&pdev->dev);
  1052. if (!pm_runtime_status_suspended(&pdev->dev))
  1053. tegra_spi_runtime_suspend(&pdev->dev);
  1054. return 0;
  1055. }
  1056. #ifdef CONFIG_PM_SLEEP
  1057. static int tegra_spi_suspend(struct device *dev)
  1058. {
  1059. struct spi_master *master = dev_get_drvdata(dev);
  1060. return spi_master_suspend(master);
  1061. }
  1062. static int tegra_spi_resume(struct device *dev)
  1063. {
  1064. struct spi_master *master = dev_get_drvdata(dev);
  1065. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1066. int ret;
  1067. ret = pm_runtime_get_sync(dev);
  1068. if (ret < 0) {
  1069. pm_runtime_put_noidle(dev);
  1070. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1071. return ret;
  1072. }
  1073. tegra_spi_writel(tspi, tspi->command1_reg, SPI_COMMAND1);
  1074. pm_runtime_put(dev);
  1075. return spi_master_resume(master);
  1076. }
  1077. #endif
  1078. static int tegra_spi_runtime_suspend(struct device *dev)
  1079. {
  1080. struct spi_master *master = dev_get_drvdata(dev);
  1081. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1082. /* Flush all write which are in PPSB queue by reading back */
  1083. tegra_spi_readl(tspi, SPI_COMMAND1);
  1084. clk_disable_unprepare(tspi->clk);
  1085. return 0;
  1086. }
  1087. static int tegra_spi_runtime_resume(struct device *dev)
  1088. {
  1089. struct spi_master *master = dev_get_drvdata(dev);
  1090. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1091. int ret;
  1092. ret = clk_prepare_enable(tspi->clk);
  1093. if (ret < 0) {
  1094. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1095. return ret;
  1096. }
  1097. return 0;
  1098. }
  1099. static const struct dev_pm_ops tegra_spi_pm_ops = {
  1100. SET_RUNTIME_PM_OPS(tegra_spi_runtime_suspend,
  1101. tegra_spi_runtime_resume, NULL)
  1102. SET_SYSTEM_SLEEP_PM_OPS(tegra_spi_suspend, tegra_spi_resume)
  1103. };
  1104. static struct platform_driver tegra_spi_driver = {
  1105. .driver = {
  1106. .name = "spi-tegra114",
  1107. .pm = &tegra_spi_pm_ops,
  1108. .of_match_table = tegra_spi_of_match,
  1109. },
  1110. .probe = tegra_spi_probe,
  1111. .remove = tegra_spi_remove,
  1112. };
  1113. module_platform_driver(tegra_spi_driver);
  1114. MODULE_ALIAS("platform:spi-tegra114");
  1115. MODULE_DESCRIPTION("NVIDIA Tegra114 SPI Controller Driver");
  1116. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1117. MODULE_LICENSE("GPL v2");