spi-tegra114.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  709. return ret;
  710. }
  711. spin_lock_irqsave(&tspi->lock, flags);
  712. val = tspi->def_command1_reg;
  713. if (spi->mode & SPI_CS_HIGH)
  714. val &= ~SPI_CS_POL_INACTIVE(spi->chip_select);
  715. else
  716. val |= SPI_CS_POL_INACTIVE(spi->chip_select);
  717. tspi->def_command1_reg = val;
  718. tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
  719. spin_unlock_irqrestore(&tspi->lock, flags);
  720. pm_runtime_put(tspi->dev);
  721. return 0;
  722. }
  723. static void tegra_spi_transfer_delay(int delay)
  724. {
  725. if (!delay)
  726. return;
  727. if (delay >= 1000)
  728. mdelay(delay / 1000);
  729. udelay(delay % 1000);
  730. }
  731. static int tegra_spi_transfer_one_message(struct spi_master *master,
  732. struct spi_message *msg)
  733. {
  734. bool is_first_msg = true;
  735. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  736. struct spi_transfer *xfer;
  737. struct spi_device *spi = msg->spi;
  738. int ret;
  739. bool skip = false;
  740. msg->status = 0;
  741. msg->actual_length = 0;
  742. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  743. u32 cmd1;
  744. reinit_completion(&tspi->xfer_completion);
  745. cmd1 = tegra_spi_setup_transfer_one(spi, xfer, is_first_msg);
  746. if (!xfer->len) {
  747. ret = 0;
  748. skip = true;
  749. goto complete_xfer;
  750. }
  751. ret = tegra_spi_start_transfer_one(spi, xfer, cmd1);
  752. if (ret < 0) {
  753. dev_err(tspi->dev,
  754. "spi can not start transfer, err %d\n", ret);
  755. goto complete_xfer;
  756. }
  757. is_first_msg = false;
  758. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  759. SPI_DMA_TIMEOUT);
  760. if (WARN_ON(ret == 0)) {
  761. dev_err(tspi->dev,
  762. "spi transfer timeout, err %d\n", ret);
  763. if (tspi->is_curr_dma_xfer &&
  764. (tspi->cur_direction & DATA_DIR_TX))
  765. dmaengine_terminate_all(tspi->tx_dma_chan);
  766. if (tspi->is_curr_dma_xfer &&
  767. (tspi->cur_direction & DATA_DIR_RX))
  768. dmaengine_terminate_all(tspi->rx_dma_chan);
  769. ret = -EIO;
  770. tegra_spi_flush_fifos(tspi);
  771. reset_control_assert(tspi->rst);
  772. udelay(2);
  773. reset_control_deassert(tspi->rst);
  774. goto complete_xfer;
  775. }
  776. if (tspi->tx_status || tspi->rx_status) {
  777. dev_err(tspi->dev, "Error in Transfer\n");
  778. ret = -EIO;
  779. goto complete_xfer;
  780. }
  781. msg->actual_length += xfer->len;
  782. complete_xfer:
  783. if (ret < 0 || skip) {
  784. tegra_spi_writel(tspi, tspi->def_command1_reg,
  785. SPI_COMMAND1);
  786. tegra_spi_transfer_delay(xfer->delay_usecs);
  787. goto exit;
  788. } else if (list_is_last(&xfer->transfer_list,
  789. &msg->transfers)) {
  790. if (xfer->cs_change)
  791. tspi->cs_control = spi;
  792. else {
  793. tegra_spi_writel(tspi, tspi->def_command1_reg,
  794. SPI_COMMAND1);
  795. tegra_spi_transfer_delay(xfer->delay_usecs);
  796. }
  797. } else if (xfer->cs_change) {
  798. tegra_spi_writel(tspi, tspi->def_command1_reg,
  799. SPI_COMMAND1);
  800. tegra_spi_transfer_delay(xfer->delay_usecs);
  801. }
  802. }
  803. ret = 0;
  804. exit:
  805. msg->status = ret;
  806. spi_finalize_current_message(master);
  807. return ret;
  808. }
  809. static irqreturn_t handle_cpu_based_xfer(struct tegra_spi_data *tspi)
  810. {
  811. struct spi_transfer *t = tspi->curr_xfer;
  812. unsigned long flags;
  813. spin_lock_irqsave(&tspi->lock, flags);
  814. if (tspi->tx_status || tspi->rx_status) {
  815. dev_err(tspi->dev, "CpuXfer ERROR bit set 0x%x\n",
  816. tspi->status_reg);
  817. dev_err(tspi->dev, "CpuXfer 0x%08x:0x%08x\n",
  818. tspi->command1_reg, tspi->dma_control_reg);
  819. tegra_spi_flush_fifos(tspi);
  820. reset_control_assert(tspi->rst);
  821. udelay(2);
  822. reset_control_deassert(tspi->rst);
  823. complete(&tspi->xfer_completion);
  824. goto exit;
  825. }
  826. if (tspi->cur_direction & DATA_DIR_RX)
  827. tegra_spi_read_rx_fifo_to_client_rxbuf(tspi, t);
  828. if (tspi->cur_direction & DATA_DIR_TX)
  829. tspi->cur_pos = tspi->cur_tx_pos;
  830. else
  831. tspi->cur_pos = tspi->cur_rx_pos;
  832. if (tspi->cur_pos == t->len) {
  833. complete(&tspi->xfer_completion);
  834. goto exit;
  835. }
  836. tegra_spi_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  837. tegra_spi_start_cpu_based_transfer(tspi, t);
  838. exit:
  839. spin_unlock_irqrestore(&tspi->lock, flags);
  840. return IRQ_HANDLED;
  841. }
  842. static irqreturn_t handle_dma_based_xfer(struct tegra_spi_data *tspi)
  843. {
  844. struct spi_transfer *t = tspi->curr_xfer;
  845. long wait_status;
  846. int err = 0;
  847. unsigned total_fifo_words;
  848. unsigned long flags;
  849. /* Abort dmas if any error */
  850. if (tspi->cur_direction & DATA_DIR_TX) {
  851. if (tspi->tx_status) {
  852. dmaengine_terminate_all(tspi->tx_dma_chan);
  853. err += 1;
  854. } else {
  855. wait_status = wait_for_completion_interruptible_timeout(
  856. &tspi->tx_dma_complete, SPI_DMA_TIMEOUT);
  857. if (wait_status <= 0) {
  858. dmaengine_terminate_all(tspi->tx_dma_chan);
  859. dev_err(tspi->dev, "TxDma Xfer failed\n");
  860. err += 1;
  861. }
  862. }
  863. }
  864. if (tspi->cur_direction & DATA_DIR_RX) {
  865. if (tspi->rx_status) {
  866. dmaengine_terminate_all(tspi->rx_dma_chan);
  867. err += 2;
  868. } else {
  869. wait_status = wait_for_completion_interruptible_timeout(
  870. &tspi->rx_dma_complete, SPI_DMA_TIMEOUT);
  871. if (wait_status <= 0) {
  872. dmaengine_terminate_all(tspi->rx_dma_chan);
  873. dev_err(tspi->dev, "RxDma Xfer failed\n");
  874. err += 2;
  875. }
  876. }
  877. }
  878. spin_lock_irqsave(&tspi->lock, flags);
  879. if (err) {
  880. dev_err(tspi->dev, "DmaXfer: ERROR bit set 0x%x\n",
  881. tspi->status_reg);
  882. dev_err(tspi->dev, "DmaXfer 0x%08x:0x%08x\n",
  883. tspi->command1_reg, tspi->dma_control_reg);
  884. tegra_spi_flush_fifos(tspi);
  885. reset_control_assert(tspi->rst);
  886. udelay(2);
  887. reset_control_deassert(tspi->rst);
  888. complete(&tspi->xfer_completion);
  889. spin_unlock_irqrestore(&tspi->lock, flags);
  890. return IRQ_HANDLED;
  891. }
  892. if (tspi->cur_direction & DATA_DIR_RX)
  893. tegra_spi_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  894. if (tspi->cur_direction & DATA_DIR_TX)
  895. tspi->cur_pos = tspi->cur_tx_pos;
  896. else
  897. tspi->cur_pos = tspi->cur_rx_pos;
  898. if (tspi->cur_pos == t->len) {
  899. complete(&tspi->xfer_completion);
  900. goto exit;
  901. }
  902. /* Continue transfer in current message */
  903. total_fifo_words = tegra_spi_calculate_curr_xfer_param(tspi->cur_spi,
  904. tspi, t);
  905. if (total_fifo_words > SPI_FIFO_DEPTH)
  906. err = tegra_spi_start_dma_based_transfer(tspi, t);
  907. else
  908. err = tegra_spi_start_cpu_based_transfer(tspi, t);
  909. exit:
  910. spin_unlock_irqrestore(&tspi->lock, flags);
  911. return IRQ_HANDLED;
  912. }
  913. static irqreturn_t tegra_spi_isr_thread(int irq, void *context_data)
  914. {
  915. struct tegra_spi_data *tspi = context_data;
  916. if (!tspi->is_curr_dma_xfer)
  917. return handle_cpu_based_xfer(tspi);
  918. return handle_dma_based_xfer(tspi);
  919. }
  920. static irqreturn_t tegra_spi_isr(int irq, void *context_data)
  921. {
  922. struct tegra_spi_data *tspi = context_data;
  923. tspi->status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  924. if (tspi->cur_direction & DATA_DIR_TX)
  925. tspi->tx_status = tspi->status_reg &
  926. (SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF);
  927. if (tspi->cur_direction & DATA_DIR_RX)
  928. tspi->rx_status = tspi->status_reg &
  929. (SPI_RX_FIFO_OVF | SPI_RX_FIFO_UNF);
  930. tegra_spi_clear_status(tspi);
  931. return IRQ_WAKE_THREAD;
  932. }
  933. static const struct of_device_id tegra_spi_of_match[] = {
  934. { .compatible = "nvidia,tegra114-spi", },
  935. {}
  936. };
  937. MODULE_DEVICE_TABLE(of, tegra_spi_of_match);
  938. static int tegra_spi_probe(struct platform_device *pdev)
  939. {
  940. struct spi_master *master;
  941. struct tegra_spi_data *tspi;
  942. struct resource *r;
  943. int ret, spi_irq;
  944. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  945. if (!master) {
  946. dev_err(&pdev->dev, "master allocation failed\n");
  947. return -ENOMEM;
  948. }
  949. platform_set_drvdata(pdev, master);
  950. tspi = spi_master_get_devdata(master);
  951. if (of_property_read_u32(pdev->dev.of_node, "spi-max-frequency",
  952. &master->max_speed_hz))
  953. master->max_speed_hz = 25000000; /* 25MHz */
  954. /* the spi->mode bits understood by this driver: */
  955. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  956. master->setup = tegra_spi_setup;
  957. master->transfer_one_message = tegra_spi_transfer_one_message;
  958. master->num_chipselect = MAX_CHIP_SELECT;
  959. master->auto_runtime_pm = true;
  960. tspi->master = master;
  961. tspi->dev = &pdev->dev;
  962. spin_lock_init(&tspi->lock);
  963. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  964. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  965. if (IS_ERR(tspi->base)) {
  966. ret = PTR_ERR(tspi->base);
  967. goto exit_free_master;
  968. }
  969. tspi->phys = r->start;
  970. spi_irq = platform_get_irq(pdev, 0);
  971. tspi->irq = spi_irq;
  972. tspi->clk = devm_clk_get(&pdev->dev, "spi");
  973. if (IS_ERR(tspi->clk)) {
  974. dev_err(&pdev->dev, "can not get clock\n");
  975. ret = PTR_ERR(tspi->clk);
  976. goto exit_free_master;
  977. }
  978. tspi->rst = devm_reset_control_get_exclusive(&pdev->dev, "spi");
  979. if (IS_ERR(tspi->rst)) {
  980. dev_err(&pdev->dev, "can not get reset\n");
  981. ret = PTR_ERR(tspi->rst);
  982. goto exit_free_master;
  983. }
  984. tspi->max_buf_size = SPI_FIFO_DEPTH << 2;
  985. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  986. ret = tegra_spi_init_dma_param(tspi, true);
  987. if (ret < 0)
  988. goto exit_free_master;
  989. ret = tegra_spi_init_dma_param(tspi, false);
  990. if (ret < 0)
  991. goto exit_rx_dma_free;
  992. tspi->max_buf_size = tspi->dma_buf_size;
  993. init_completion(&tspi->tx_dma_complete);
  994. init_completion(&tspi->rx_dma_complete);
  995. init_completion(&tspi->xfer_completion);
  996. pm_runtime_enable(&pdev->dev);
  997. if (!pm_runtime_enabled(&pdev->dev)) {
  998. ret = tegra_spi_runtime_resume(&pdev->dev);
  999. if (ret)
  1000. goto exit_pm_disable;
  1001. }
  1002. ret = pm_runtime_get_sync(&pdev->dev);
  1003. if (ret < 0) {
  1004. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  1005. goto exit_pm_disable;
  1006. }
  1007. reset_control_assert(tspi->rst);
  1008. udelay(2);
  1009. reset_control_deassert(tspi->rst);
  1010. tspi->def_command1_reg = SPI_M_S;
  1011. tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
  1012. pm_runtime_put(&pdev->dev);
  1013. ret = request_threaded_irq(tspi->irq, tegra_spi_isr,
  1014. tegra_spi_isr_thread, IRQF_ONESHOT,
  1015. dev_name(&pdev->dev), tspi);
  1016. if (ret < 0) {
  1017. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  1018. tspi->irq);
  1019. goto exit_pm_disable;
  1020. }
  1021. master->dev.of_node = pdev->dev.of_node;
  1022. ret = devm_spi_register_master(&pdev->dev, master);
  1023. if (ret < 0) {
  1024. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  1025. goto exit_free_irq;
  1026. }
  1027. return ret;
  1028. exit_free_irq:
  1029. free_irq(spi_irq, tspi);
  1030. exit_pm_disable:
  1031. pm_runtime_disable(&pdev->dev);
  1032. if (!pm_runtime_status_suspended(&pdev->dev))
  1033. tegra_spi_runtime_suspend(&pdev->dev);
  1034. tegra_spi_deinit_dma_param(tspi, false);
  1035. exit_rx_dma_free:
  1036. tegra_spi_deinit_dma_param(tspi, true);
  1037. exit_free_master:
  1038. spi_master_put(master);
  1039. return ret;
  1040. }
  1041. static int tegra_spi_remove(struct platform_device *pdev)
  1042. {
  1043. struct spi_master *master = platform_get_drvdata(pdev);
  1044. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1045. free_irq(tspi->irq, tspi);
  1046. if (tspi->tx_dma_chan)
  1047. tegra_spi_deinit_dma_param(tspi, false);
  1048. if (tspi->rx_dma_chan)
  1049. tegra_spi_deinit_dma_param(tspi, true);
  1050. pm_runtime_disable(&pdev->dev);
  1051. if (!pm_runtime_status_suspended(&pdev->dev))
  1052. tegra_spi_runtime_suspend(&pdev->dev);
  1053. return 0;
  1054. }
  1055. #ifdef CONFIG_PM_SLEEP
  1056. static int tegra_spi_suspend(struct device *dev)
  1057. {
  1058. struct spi_master *master = dev_get_drvdata(dev);
  1059. return spi_master_suspend(master);
  1060. }
  1061. static int tegra_spi_resume(struct device *dev)
  1062. {
  1063. struct spi_master *master = dev_get_drvdata(dev);
  1064. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1065. int ret;
  1066. ret = pm_runtime_get_sync(dev);
  1067. if (ret < 0) {
  1068. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1069. return ret;
  1070. }
  1071. tegra_spi_writel(tspi, tspi->command1_reg, SPI_COMMAND1);
  1072. pm_runtime_put(dev);
  1073. return spi_master_resume(master);
  1074. }
  1075. #endif
  1076. static int tegra_spi_runtime_suspend(struct device *dev)
  1077. {
  1078. struct spi_master *master = dev_get_drvdata(dev);
  1079. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1080. /* Flush all write which are in PPSB queue by reading back */
  1081. tegra_spi_readl(tspi, SPI_COMMAND1);
  1082. clk_disable_unprepare(tspi->clk);
  1083. return 0;
  1084. }
  1085. static int tegra_spi_runtime_resume(struct device *dev)
  1086. {
  1087. struct spi_master *master = dev_get_drvdata(dev);
  1088. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1089. int ret;
  1090. ret = clk_prepare_enable(tspi->clk);
  1091. if (ret < 0) {
  1092. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1093. return ret;
  1094. }
  1095. return 0;
  1096. }
  1097. static const struct dev_pm_ops tegra_spi_pm_ops = {
  1098. SET_RUNTIME_PM_OPS(tegra_spi_runtime_suspend,
  1099. tegra_spi_runtime_resume, NULL)
  1100. SET_SYSTEM_SLEEP_PM_OPS(tegra_spi_suspend, tegra_spi_resume)
  1101. };
  1102. static struct platform_driver tegra_spi_driver = {
  1103. .driver = {
  1104. .name = "spi-tegra114",
  1105. .pm = &tegra_spi_pm_ops,
  1106. .of_match_table = tegra_spi_of_match,
  1107. },
  1108. .probe = tegra_spi_probe,
  1109. .remove = tegra_spi_remove,
  1110. };
  1111. module_platform_driver(tegra_spi_driver);
  1112. MODULE_ALIAS("platform:spi-tegra114");
  1113. MODULE_DESCRIPTION("NVIDIA Tegra114 SPI Controller Driver");
  1114. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1115. MODULE_LICENSE("GPL v2");