spi-tegra20-slink.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /*
  2. * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
  3. *
  4. * Copyright (c) 2012, 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/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/kernel.h>
  29. #include <linux/kthread.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/of.h>
  34. #include <linux/of_device.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/clk/tegra.h>
  37. #define SLINK_COMMAND 0x000
  38. #define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  39. #define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
  40. #define SLINK_BOTH_EN (1 << 10)
  41. #define SLINK_CS_SW (1 << 11)
  42. #define SLINK_CS_VALUE (1 << 12)
  43. #define SLINK_CS_POLARITY (1 << 13)
  44. #define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
  45. #define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
  46. #define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
  47. #define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
  48. #define SLINK_IDLE_SDA_MASK (3 << 16)
  49. #define SLINK_CS_POLARITY1 (1 << 20)
  50. #define SLINK_CK_SDA (1 << 21)
  51. #define SLINK_CS_POLARITY2 (1 << 22)
  52. #define SLINK_CS_POLARITY3 (1 << 23)
  53. #define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
  54. #define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
  55. #define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
  56. #define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
  57. #define SLINK_IDLE_SCLK_MASK (3 << 24)
  58. #define SLINK_M_S (1 << 28)
  59. #define SLINK_WAIT (1 << 29)
  60. #define SLINK_GO (1 << 30)
  61. #define SLINK_ENB (1 << 31)
  62. #define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
  63. #define SLINK_COMMAND2 0x004
  64. #define SLINK_LSBFE (1 << 0)
  65. #define SLINK_SSOE (1 << 1)
  66. #define SLINK_SPIE (1 << 4)
  67. #define SLINK_BIDIROE (1 << 6)
  68. #define SLINK_MODFEN (1 << 7)
  69. #define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
  70. #define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
  71. #define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
  72. #define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
  73. #define SLINK_FIFO_REFILLS_0 (0 << 22)
  74. #define SLINK_FIFO_REFILLS_1 (1 << 22)
  75. #define SLINK_FIFO_REFILLS_2 (2 << 22)
  76. #define SLINK_FIFO_REFILLS_3 (3 << 22)
  77. #define SLINK_FIFO_REFILLS_MASK (3 << 22)
  78. #define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
  79. #define SLINK_SPC0 (1 << 29)
  80. #define SLINK_TXEN (1 << 30)
  81. #define SLINK_RXEN (1 << 31)
  82. #define SLINK_STATUS 0x008
  83. #define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
  84. #define SLINK_WORD(val) (((val) >> 5) & 0x1f)
  85. #define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
  86. #define SLINK_MODF (1 << 16)
  87. #define SLINK_RX_UNF (1 << 18)
  88. #define SLINK_TX_OVF (1 << 19)
  89. #define SLINK_TX_FULL (1 << 20)
  90. #define SLINK_TX_EMPTY (1 << 21)
  91. #define SLINK_RX_FULL (1 << 22)
  92. #define SLINK_RX_EMPTY (1 << 23)
  93. #define SLINK_TX_UNF (1 << 24)
  94. #define SLINK_RX_OVF (1 << 25)
  95. #define SLINK_TX_FLUSH (1 << 26)
  96. #define SLINK_RX_FLUSH (1 << 27)
  97. #define SLINK_SCLK (1 << 28)
  98. #define SLINK_ERR (1 << 29)
  99. #define SLINK_RDY (1 << 30)
  100. #define SLINK_BSY (1 << 31)
  101. #define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \
  102. SLINK_TX_UNF | SLINK_RX_OVF)
  103. #define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)
  104. #define SLINK_MAS_DATA 0x010
  105. #define SLINK_SLAVE_DATA 0x014
  106. #define SLINK_DMA_CTL 0x018
  107. #define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
  108. #define SLINK_TX_TRIG_1 (0 << 16)
  109. #define SLINK_TX_TRIG_4 (1 << 16)
  110. #define SLINK_TX_TRIG_8 (2 << 16)
  111. #define SLINK_TX_TRIG_16 (3 << 16)
  112. #define SLINK_TX_TRIG_MASK (3 << 16)
  113. #define SLINK_RX_TRIG_1 (0 << 18)
  114. #define SLINK_RX_TRIG_4 (1 << 18)
  115. #define SLINK_RX_TRIG_8 (2 << 18)
  116. #define SLINK_RX_TRIG_16 (3 << 18)
  117. #define SLINK_RX_TRIG_MASK (3 << 18)
  118. #define SLINK_PACKED (1 << 20)
  119. #define SLINK_PACK_SIZE_4 (0 << 21)
  120. #define SLINK_PACK_SIZE_8 (1 << 21)
  121. #define SLINK_PACK_SIZE_16 (2 << 21)
  122. #define SLINK_PACK_SIZE_32 (3 << 21)
  123. #define SLINK_PACK_SIZE_MASK (3 << 21)
  124. #define SLINK_IE_TXC (1 << 26)
  125. #define SLINK_IE_RXC (1 << 27)
  126. #define SLINK_DMA_EN (1 << 31)
  127. #define SLINK_STATUS2 0x01c
  128. #define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
  129. #define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)
  130. #define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)
  131. #define SLINK_TX_FIFO 0x100
  132. #define SLINK_RX_FIFO 0x180
  133. #define DATA_DIR_TX (1 << 0)
  134. #define DATA_DIR_RX (1 << 1)
  135. #define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))
  136. #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
  137. #define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)
  138. #define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)
  139. #define SLINK_STATUS2_RESET \
  140. (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
  141. #define MAX_CHIP_SELECT 4
  142. #define SLINK_FIFO_DEPTH 32
  143. struct tegra_slink_chip_data {
  144. bool cs_hold_time;
  145. };
  146. struct tegra_slink_data {
  147. struct device *dev;
  148. struct spi_master *master;
  149. const struct tegra_slink_chip_data *chip_data;
  150. spinlock_t lock;
  151. struct clk *clk;
  152. void __iomem *base;
  153. phys_addr_t phys;
  154. unsigned irq;
  155. int dma_req_sel;
  156. u32 spi_max_frequency;
  157. u32 cur_speed;
  158. struct spi_device *cur_spi;
  159. unsigned cur_pos;
  160. unsigned cur_len;
  161. unsigned words_per_32bit;
  162. unsigned bytes_per_word;
  163. unsigned curr_dma_words;
  164. unsigned cur_direction;
  165. unsigned cur_rx_pos;
  166. unsigned cur_tx_pos;
  167. unsigned dma_buf_size;
  168. unsigned max_buf_size;
  169. bool is_curr_dma_xfer;
  170. struct completion rx_dma_complete;
  171. struct completion tx_dma_complete;
  172. u32 tx_status;
  173. u32 rx_status;
  174. u32 status_reg;
  175. bool is_packed;
  176. unsigned long packed_size;
  177. u32 command_reg;
  178. u32 command2_reg;
  179. u32 dma_control_reg;
  180. u32 def_command_reg;
  181. u32 def_command2_reg;
  182. struct completion xfer_completion;
  183. struct spi_transfer *curr_xfer;
  184. struct dma_chan *rx_dma_chan;
  185. u32 *rx_dma_buf;
  186. dma_addr_t rx_dma_phys;
  187. struct dma_async_tx_descriptor *rx_dma_desc;
  188. struct dma_chan *tx_dma_chan;
  189. u32 *tx_dma_buf;
  190. dma_addr_t tx_dma_phys;
  191. struct dma_async_tx_descriptor *tx_dma_desc;
  192. };
  193. static int tegra_slink_runtime_suspend(struct device *dev);
  194. static int tegra_slink_runtime_resume(struct device *dev);
  195. static inline unsigned long tegra_slink_readl(struct tegra_slink_data *tspi,
  196. unsigned long reg)
  197. {
  198. return readl(tspi->base + reg);
  199. }
  200. static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
  201. unsigned long val, unsigned long reg)
  202. {
  203. writel(val, tspi->base + reg);
  204. /* Read back register to make sure that register writes completed */
  205. if (reg != SLINK_TX_FIFO)
  206. readl(tspi->base + SLINK_MAS_DATA);
  207. }
  208. static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
  209. {
  210. unsigned long val;
  211. unsigned long val_write = 0;
  212. val = tegra_slink_readl(tspi, SLINK_STATUS);
  213. /* Write 1 to clear status register */
  214. val_write = SLINK_RDY | SLINK_FIFO_ERROR;
  215. tegra_slink_writel(tspi, val_write, SLINK_STATUS);
  216. }
  217. static unsigned long tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
  218. struct spi_transfer *t)
  219. {
  220. unsigned long val;
  221. switch (tspi->bytes_per_word) {
  222. case 0:
  223. val = SLINK_PACK_SIZE_4;
  224. break;
  225. case 1:
  226. val = SLINK_PACK_SIZE_8;
  227. break;
  228. case 2:
  229. val = SLINK_PACK_SIZE_16;
  230. break;
  231. case 4:
  232. val = SLINK_PACK_SIZE_32;
  233. break;
  234. default:
  235. val = 0;
  236. }
  237. return val;
  238. }
  239. static unsigned tegra_slink_calculate_curr_xfer_param(
  240. struct spi_device *spi, struct tegra_slink_data *tspi,
  241. struct spi_transfer *t)
  242. {
  243. unsigned remain_len = t->len - tspi->cur_pos;
  244. unsigned max_word;
  245. unsigned bits_per_word ;
  246. unsigned max_len;
  247. unsigned total_fifo_words;
  248. bits_per_word = t->bits_per_word;
  249. tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
  250. if (bits_per_word == 8 || bits_per_word == 16) {
  251. tspi->is_packed = 1;
  252. tspi->words_per_32bit = 32/bits_per_word;
  253. } else {
  254. tspi->is_packed = 0;
  255. tspi->words_per_32bit = 1;
  256. }
  257. tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
  258. if (tspi->is_packed) {
  259. max_len = min(remain_len, tspi->max_buf_size);
  260. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  261. total_fifo_words = max_len/4;
  262. } else {
  263. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  264. max_word = min(max_word, tspi->max_buf_size/4);
  265. tspi->curr_dma_words = max_word;
  266. total_fifo_words = max_word;
  267. }
  268. return total_fifo_words;
  269. }
  270. static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
  271. struct tegra_slink_data *tspi, struct spi_transfer *t)
  272. {
  273. unsigned nbytes;
  274. unsigned tx_empty_count;
  275. unsigned long fifo_status;
  276. unsigned max_n_32bit;
  277. unsigned i, count;
  278. unsigned long x;
  279. unsigned int written_words;
  280. unsigned fifo_words_left;
  281. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  282. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  283. tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
  284. if (tspi->is_packed) {
  285. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  286. written_words = min(fifo_words_left, tspi->curr_dma_words);
  287. nbytes = written_words * tspi->bytes_per_word;
  288. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  289. for (count = 0; count < max_n_32bit; count++) {
  290. x = 0;
  291. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  292. x |= (*tx_buf++) << (i*8);
  293. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  294. }
  295. } else {
  296. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  297. written_words = max_n_32bit;
  298. nbytes = written_words * tspi->bytes_per_word;
  299. for (count = 0; count < max_n_32bit; count++) {
  300. x = 0;
  301. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  302. i++, nbytes--)
  303. x |= ((*tx_buf++) << i*8);
  304. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  305. }
  306. }
  307. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  308. return written_words;
  309. }
  310. static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
  311. struct tegra_slink_data *tspi, struct spi_transfer *t)
  312. {
  313. unsigned rx_full_count;
  314. unsigned long fifo_status;
  315. unsigned i, count;
  316. unsigned long x;
  317. unsigned int read_words = 0;
  318. unsigned len;
  319. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  320. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  321. rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
  322. if (tspi->is_packed) {
  323. len = tspi->curr_dma_words * tspi->bytes_per_word;
  324. for (count = 0; count < rx_full_count; count++) {
  325. x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  326. for (i = 0; len && (i < 4); i++, len--)
  327. *rx_buf++ = (x >> i*8) & 0xFF;
  328. }
  329. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  330. read_words += tspi->curr_dma_words;
  331. } else {
  332. for (count = 0; count < rx_full_count; count++) {
  333. x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  334. for (i = 0; (i < tspi->bytes_per_word); i++)
  335. *rx_buf++ = (x >> (i*8)) & 0xFF;
  336. }
  337. tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  338. read_words += rx_full_count;
  339. }
  340. return read_words;
  341. }
  342. static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
  343. struct tegra_slink_data *tspi, struct spi_transfer *t)
  344. {
  345. unsigned len;
  346. /* Make the dma buffer to read by cpu */
  347. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  348. tspi->dma_buf_size, DMA_TO_DEVICE);
  349. if (tspi->is_packed) {
  350. len = tspi->curr_dma_words * tspi->bytes_per_word;
  351. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  352. } else {
  353. unsigned int i;
  354. unsigned int count;
  355. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  356. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  357. unsigned int x;
  358. for (count = 0; count < tspi->curr_dma_words; count++) {
  359. x = 0;
  360. for (i = 0; consume && (i < tspi->bytes_per_word);
  361. i++, consume--)
  362. x |= ((*tx_buf++) << i * 8);
  363. tspi->tx_dma_buf[count] = x;
  364. }
  365. }
  366. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  367. /* Make the dma buffer to read by dma */
  368. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  369. tspi->dma_buf_size, DMA_TO_DEVICE);
  370. }
  371. static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
  372. struct tegra_slink_data *tspi, struct spi_transfer *t)
  373. {
  374. unsigned len;
  375. /* Make the dma buffer to read by cpu */
  376. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  377. tspi->dma_buf_size, DMA_FROM_DEVICE);
  378. if (tspi->is_packed) {
  379. len = tspi->curr_dma_words * tspi->bytes_per_word;
  380. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  381. } else {
  382. unsigned int i;
  383. unsigned int count;
  384. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  385. unsigned int x;
  386. unsigned int rx_mask, bits_per_word;
  387. bits_per_word = t->bits_per_word;
  388. rx_mask = (1 << bits_per_word) - 1;
  389. for (count = 0; count < tspi->curr_dma_words; count++) {
  390. x = tspi->rx_dma_buf[count];
  391. x &= rx_mask;
  392. for (i = 0; (i < tspi->bytes_per_word); i++)
  393. *rx_buf++ = (x >> (i*8)) & 0xFF;
  394. }
  395. }
  396. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  397. /* Make the dma buffer to read by dma */
  398. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  399. tspi->dma_buf_size, DMA_FROM_DEVICE);
  400. }
  401. static void tegra_slink_dma_complete(void *args)
  402. {
  403. struct completion *dma_complete = args;
  404. complete(dma_complete);
  405. }
  406. static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
  407. {
  408. INIT_COMPLETION(tspi->tx_dma_complete);
  409. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  410. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  411. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  412. if (!tspi->tx_dma_desc) {
  413. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  414. return -EIO;
  415. }
  416. tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
  417. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  418. dmaengine_submit(tspi->tx_dma_desc);
  419. dma_async_issue_pending(tspi->tx_dma_chan);
  420. return 0;
  421. }
  422. static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
  423. {
  424. INIT_COMPLETION(tspi->rx_dma_complete);
  425. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  426. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  427. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  428. if (!tspi->rx_dma_desc) {
  429. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  430. return -EIO;
  431. }
  432. tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
  433. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  434. dmaengine_submit(tspi->rx_dma_desc);
  435. dma_async_issue_pending(tspi->rx_dma_chan);
  436. return 0;
  437. }
  438. static int tegra_slink_start_dma_based_transfer(
  439. struct tegra_slink_data *tspi, struct spi_transfer *t)
  440. {
  441. unsigned long val;
  442. unsigned long test_val;
  443. unsigned int len;
  444. int ret = 0;
  445. unsigned long status;
  446. /* Make sure that Rx and Tx fifo are empty */
  447. status = tegra_slink_readl(tspi, SLINK_STATUS);
  448. if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
  449. dev_err(tspi->dev,
  450. "Rx/Tx fifo are not empty status 0x%08lx\n", status);
  451. return -EIO;
  452. }
  453. val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
  454. val |= tspi->packed_size;
  455. if (tspi->is_packed)
  456. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  457. 4) * 4;
  458. else
  459. len = tspi->curr_dma_words * 4;
  460. /* Set attention level based on length of transfer */
  461. if (len & 0xF)
  462. val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
  463. else if (((len) >> 4) & 0x1)
  464. val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
  465. else
  466. val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
  467. if (tspi->cur_direction & DATA_DIR_TX)
  468. val |= SLINK_IE_TXC;
  469. if (tspi->cur_direction & DATA_DIR_RX)
  470. val |= SLINK_IE_RXC;
  471. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  472. tspi->dma_control_reg = val;
  473. if (tspi->cur_direction & DATA_DIR_TX) {
  474. tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
  475. wmb();
  476. ret = tegra_slink_start_tx_dma(tspi, len);
  477. if (ret < 0) {
  478. dev_err(tspi->dev,
  479. "Starting tx dma failed, err %d\n", ret);
  480. return ret;
  481. }
  482. /* Wait for tx fifo to be fill before starting slink */
  483. test_val = tegra_slink_readl(tspi, SLINK_STATUS);
  484. while (!(test_val & SLINK_TX_FULL))
  485. test_val = tegra_slink_readl(tspi, SLINK_STATUS);
  486. }
  487. if (tspi->cur_direction & DATA_DIR_RX) {
  488. /* Make the dma buffer to read by dma */
  489. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  490. tspi->dma_buf_size, DMA_FROM_DEVICE);
  491. ret = tegra_slink_start_rx_dma(tspi, len);
  492. if (ret < 0) {
  493. dev_err(tspi->dev,
  494. "Starting rx dma failed, err %d\n", ret);
  495. if (tspi->cur_direction & DATA_DIR_TX)
  496. dmaengine_terminate_all(tspi->tx_dma_chan);
  497. return ret;
  498. }
  499. }
  500. tspi->is_curr_dma_xfer = true;
  501. if (tspi->is_packed) {
  502. val |= SLINK_PACKED;
  503. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  504. /* HW need small delay after settign Packed mode */
  505. udelay(1);
  506. }
  507. tspi->dma_control_reg = val;
  508. val |= SLINK_DMA_EN;
  509. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  510. return ret;
  511. }
  512. static int tegra_slink_start_cpu_based_transfer(
  513. struct tegra_slink_data *tspi, struct spi_transfer *t)
  514. {
  515. unsigned long val;
  516. unsigned cur_words;
  517. val = tspi->packed_size;
  518. if (tspi->cur_direction & DATA_DIR_TX)
  519. val |= SLINK_IE_TXC;
  520. if (tspi->cur_direction & DATA_DIR_RX)
  521. val |= SLINK_IE_RXC;
  522. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  523. tspi->dma_control_reg = val;
  524. if (tspi->cur_direction & DATA_DIR_TX)
  525. cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
  526. else
  527. cur_words = tspi->curr_dma_words;
  528. val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
  529. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  530. tspi->dma_control_reg = val;
  531. tspi->is_curr_dma_xfer = false;
  532. if (tspi->is_packed) {
  533. val |= SLINK_PACKED;
  534. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  535. udelay(1);
  536. wmb();
  537. }
  538. tspi->dma_control_reg = val;
  539. val |= SLINK_DMA_EN;
  540. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  541. return 0;
  542. }
  543. static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
  544. bool dma_to_memory)
  545. {
  546. struct dma_chan *dma_chan;
  547. u32 *dma_buf;
  548. dma_addr_t dma_phys;
  549. int ret;
  550. struct dma_slave_config dma_sconfig;
  551. dma_cap_mask_t mask;
  552. dma_cap_zero(mask);
  553. dma_cap_set(DMA_SLAVE, mask);
  554. dma_chan = dma_request_channel(mask, NULL, NULL);
  555. if (!dma_chan) {
  556. dev_err(tspi->dev,
  557. "Dma channel is not available, will try later\n");
  558. return -EPROBE_DEFER;
  559. }
  560. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  561. &dma_phys, GFP_KERNEL);
  562. if (!dma_buf) {
  563. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  564. dma_release_channel(dma_chan);
  565. return -ENOMEM;
  566. }
  567. dma_sconfig.slave_id = tspi->dma_req_sel;
  568. if (dma_to_memory) {
  569. dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
  570. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  571. dma_sconfig.src_maxburst = 0;
  572. } else {
  573. dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
  574. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  575. dma_sconfig.dst_maxburst = 0;
  576. }
  577. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  578. if (ret)
  579. goto scrub;
  580. if (dma_to_memory) {
  581. tspi->rx_dma_chan = dma_chan;
  582. tspi->rx_dma_buf = dma_buf;
  583. tspi->rx_dma_phys = dma_phys;
  584. } else {
  585. tspi->tx_dma_chan = dma_chan;
  586. tspi->tx_dma_buf = dma_buf;
  587. tspi->tx_dma_phys = dma_phys;
  588. }
  589. return 0;
  590. scrub:
  591. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  592. dma_release_channel(dma_chan);
  593. return ret;
  594. }
  595. static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
  596. bool dma_to_memory)
  597. {
  598. u32 *dma_buf;
  599. dma_addr_t dma_phys;
  600. struct dma_chan *dma_chan;
  601. if (dma_to_memory) {
  602. dma_buf = tspi->rx_dma_buf;
  603. dma_chan = tspi->rx_dma_chan;
  604. dma_phys = tspi->rx_dma_phys;
  605. tspi->rx_dma_chan = NULL;
  606. tspi->rx_dma_buf = NULL;
  607. } else {
  608. dma_buf = tspi->tx_dma_buf;
  609. dma_chan = tspi->tx_dma_chan;
  610. dma_phys = tspi->tx_dma_phys;
  611. tspi->tx_dma_buf = NULL;
  612. tspi->tx_dma_chan = NULL;
  613. }
  614. if (!dma_chan)
  615. return;
  616. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  617. dma_release_channel(dma_chan);
  618. }
  619. static int tegra_slink_start_transfer_one(struct spi_device *spi,
  620. struct spi_transfer *t, bool is_first_of_msg,
  621. bool is_single_xfer)
  622. {
  623. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  624. u32 speed;
  625. u8 bits_per_word;
  626. unsigned total_fifo_words;
  627. int ret;
  628. unsigned long command;
  629. unsigned long command2;
  630. bits_per_word = t->bits_per_word;
  631. speed = t->speed_hz;
  632. if (speed != tspi->cur_speed) {
  633. clk_set_rate(tspi->clk, speed * 4);
  634. tspi->cur_speed = speed;
  635. }
  636. tspi->cur_spi = spi;
  637. tspi->cur_pos = 0;
  638. tspi->cur_rx_pos = 0;
  639. tspi->cur_tx_pos = 0;
  640. tspi->curr_xfer = t;
  641. total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
  642. if (is_first_of_msg) {
  643. tegra_slink_clear_status(tspi);
  644. command = tspi->def_command_reg;
  645. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  646. command |= SLINK_CS_SW | SLINK_CS_VALUE;
  647. command2 = tspi->def_command2_reg;
  648. command2 |= SLINK_SS_EN_CS(spi->chip_select);
  649. command &= ~SLINK_MODES;
  650. if (spi->mode & SPI_CPHA)
  651. command |= SLINK_CK_SDA;
  652. if (spi->mode & SPI_CPOL)
  653. command |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  654. else
  655. command |= SLINK_IDLE_SCLK_DRIVE_LOW;
  656. } else {
  657. command = tspi->command_reg;
  658. command &= ~SLINK_BIT_LENGTH(~0);
  659. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  660. command2 = tspi->command2_reg;
  661. command2 &= ~(SLINK_RXEN | SLINK_TXEN);
  662. }
  663. tegra_slink_writel(tspi, command, SLINK_COMMAND);
  664. tspi->command_reg = command;
  665. tspi->cur_direction = 0;
  666. if (t->rx_buf) {
  667. command2 |= SLINK_RXEN;
  668. tspi->cur_direction |= DATA_DIR_RX;
  669. }
  670. if (t->tx_buf) {
  671. command2 |= SLINK_TXEN;
  672. tspi->cur_direction |= DATA_DIR_TX;
  673. }
  674. tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
  675. tspi->command2_reg = command2;
  676. if (total_fifo_words > SLINK_FIFO_DEPTH)
  677. ret = tegra_slink_start_dma_based_transfer(tspi, t);
  678. else
  679. ret = tegra_slink_start_cpu_based_transfer(tspi, t);
  680. return ret;
  681. }
  682. static int tegra_slink_setup(struct spi_device *spi)
  683. {
  684. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  685. unsigned long val;
  686. unsigned long flags;
  687. int ret;
  688. unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {
  689. SLINK_CS_POLARITY,
  690. SLINK_CS_POLARITY1,
  691. SLINK_CS_POLARITY2,
  692. SLINK_CS_POLARITY3,
  693. };
  694. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  695. spi->bits_per_word,
  696. spi->mode & SPI_CPOL ? "" : "~",
  697. spi->mode & SPI_CPHA ? "" : "~",
  698. spi->max_speed_hz);
  699. BUG_ON(spi->chip_select >= MAX_CHIP_SELECT);
  700. /* Set speed to the spi max fequency if spi device has not set */
  701. spi->max_speed_hz = spi->max_speed_hz ? : tspi->spi_max_frequency;
  702. ret = pm_runtime_get_sync(tspi->dev);
  703. if (ret < 0) {
  704. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  705. return ret;
  706. }
  707. spin_lock_irqsave(&tspi->lock, flags);
  708. val = tspi->def_command_reg;
  709. if (spi->mode & SPI_CS_HIGH)
  710. val |= cs_pol_bit[spi->chip_select];
  711. else
  712. val &= ~cs_pol_bit[spi->chip_select];
  713. tspi->def_command_reg = val;
  714. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  715. spin_unlock_irqrestore(&tspi->lock, flags);
  716. pm_runtime_put(tspi->dev);
  717. return 0;
  718. }
  719. static int tegra_slink_transfer_one_message(struct spi_master *master,
  720. struct spi_message *msg)
  721. {
  722. bool is_first_msg = true;
  723. int single_xfer;
  724. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  725. struct spi_transfer *xfer;
  726. struct spi_device *spi = msg->spi;
  727. int ret;
  728. msg->status = 0;
  729. msg->actual_length = 0;
  730. single_xfer = list_is_singular(&msg->transfers);
  731. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  732. INIT_COMPLETION(tspi->xfer_completion);
  733. ret = tegra_slink_start_transfer_one(spi, xfer,
  734. is_first_msg, single_xfer);
  735. if (ret < 0) {
  736. dev_err(tspi->dev,
  737. "spi can not start transfer, err %d\n", ret);
  738. goto exit;
  739. }
  740. is_first_msg = false;
  741. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  742. SLINK_DMA_TIMEOUT);
  743. if (WARN_ON(ret == 0)) {
  744. dev_err(tspi->dev,
  745. "spi trasfer timeout, err %d\n", ret);
  746. ret = -EIO;
  747. goto exit;
  748. }
  749. if (tspi->tx_status || tspi->rx_status) {
  750. dev_err(tspi->dev, "Error in Transfer\n");
  751. ret = -EIO;
  752. goto exit;
  753. }
  754. msg->actual_length += xfer->len;
  755. if (xfer->cs_change && xfer->delay_usecs) {
  756. tegra_slink_writel(tspi, tspi->def_command_reg,
  757. SLINK_COMMAND);
  758. udelay(xfer->delay_usecs);
  759. }
  760. }
  761. ret = 0;
  762. exit:
  763. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  764. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  765. msg->status = ret;
  766. spi_finalize_current_message(master);
  767. return ret;
  768. }
  769. static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  770. {
  771. struct spi_transfer *t = tspi->curr_xfer;
  772. unsigned long flags;
  773. spin_lock_irqsave(&tspi->lock, flags);
  774. if (tspi->tx_status || tspi->rx_status ||
  775. (tspi->status_reg & SLINK_BSY)) {
  776. dev_err(tspi->dev,
  777. "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
  778. dev_err(tspi->dev,
  779. "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  780. tspi->command2_reg, tspi->dma_control_reg);
  781. tegra_periph_reset_assert(tspi->clk);
  782. udelay(2);
  783. tegra_periph_reset_deassert(tspi->clk);
  784. complete(&tspi->xfer_completion);
  785. goto exit;
  786. }
  787. if (tspi->cur_direction & DATA_DIR_RX)
  788. tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  789. if (tspi->cur_direction & DATA_DIR_TX)
  790. tspi->cur_pos = tspi->cur_tx_pos;
  791. else
  792. tspi->cur_pos = tspi->cur_rx_pos;
  793. if (tspi->cur_pos == t->len) {
  794. complete(&tspi->xfer_completion);
  795. goto exit;
  796. }
  797. tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  798. tegra_slink_start_cpu_based_transfer(tspi, t);
  799. exit:
  800. spin_unlock_irqrestore(&tspi->lock, flags);
  801. return IRQ_HANDLED;
  802. }
  803. static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  804. {
  805. struct spi_transfer *t = tspi->curr_xfer;
  806. long wait_status;
  807. int err = 0;
  808. unsigned total_fifo_words;
  809. unsigned long flags;
  810. /* Abort dmas if any error */
  811. if (tspi->cur_direction & DATA_DIR_TX) {
  812. if (tspi->tx_status) {
  813. dmaengine_terminate_all(tspi->tx_dma_chan);
  814. err += 1;
  815. } else {
  816. wait_status = wait_for_completion_interruptible_timeout(
  817. &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  818. if (wait_status <= 0) {
  819. dmaengine_terminate_all(tspi->tx_dma_chan);
  820. dev_err(tspi->dev, "TxDma Xfer failed\n");
  821. err += 1;
  822. }
  823. }
  824. }
  825. if (tspi->cur_direction & DATA_DIR_RX) {
  826. if (tspi->rx_status) {
  827. dmaengine_terminate_all(tspi->rx_dma_chan);
  828. err += 2;
  829. } else {
  830. wait_status = wait_for_completion_interruptible_timeout(
  831. &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  832. if (wait_status <= 0) {
  833. dmaengine_terminate_all(tspi->rx_dma_chan);
  834. dev_err(tspi->dev, "RxDma Xfer failed\n");
  835. err += 2;
  836. }
  837. }
  838. }
  839. spin_lock_irqsave(&tspi->lock, flags);
  840. if (err) {
  841. dev_err(tspi->dev,
  842. "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
  843. dev_err(tspi->dev,
  844. "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  845. tspi->command2_reg, tspi->dma_control_reg);
  846. tegra_periph_reset_assert(tspi->clk);
  847. udelay(2);
  848. tegra_periph_reset_deassert(tspi->clk);
  849. complete(&tspi->xfer_completion);
  850. spin_unlock_irqrestore(&tspi->lock, flags);
  851. return IRQ_HANDLED;
  852. }
  853. if (tspi->cur_direction & DATA_DIR_RX)
  854. tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  855. if (tspi->cur_direction & DATA_DIR_TX)
  856. tspi->cur_pos = tspi->cur_tx_pos;
  857. else
  858. tspi->cur_pos = tspi->cur_rx_pos;
  859. if (tspi->cur_pos == t->len) {
  860. complete(&tspi->xfer_completion);
  861. goto exit;
  862. }
  863. /* Continue transfer in current message */
  864. total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  865. tspi, t);
  866. if (total_fifo_words > SLINK_FIFO_DEPTH)
  867. err = tegra_slink_start_dma_based_transfer(tspi, t);
  868. else
  869. err = tegra_slink_start_cpu_based_transfer(tspi, t);
  870. exit:
  871. spin_unlock_irqrestore(&tspi->lock, flags);
  872. return IRQ_HANDLED;
  873. }
  874. static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  875. {
  876. struct tegra_slink_data *tspi = context_data;
  877. if (!tspi->is_curr_dma_xfer)
  878. return handle_cpu_based_xfer(tspi);
  879. return handle_dma_based_xfer(tspi);
  880. }
  881. static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  882. {
  883. struct tegra_slink_data *tspi = context_data;
  884. tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  885. if (tspi->cur_direction & DATA_DIR_TX)
  886. tspi->tx_status = tspi->status_reg &
  887. (SLINK_TX_OVF | SLINK_TX_UNF);
  888. if (tspi->cur_direction & DATA_DIR_RX)
  889. tspi->rx_status = tspi->status_reg &
  890. (SLINK_RX_OVF | SLINK_RX_UNF);
  891. tegra_slink_clear_status(tspi);
  892. return IRQ_WAKE_THREAD;
  893. }
  894. static void tegra_slink_parse_dt(struct tegra_slink_data *tspi)
  895. {
  896. struct device_node *np = tspi->dev->of_node;
  897. u32 of_dma[2];
  898. if (of_property_read_u32_array(np, "nvidia,dma-request-selector",
  899. of_dma, 2) >= 0)
  900. tspi->dma_req_sel = of_dma[1];
  901. if (of_property_read_u32(np, "spi-max-frequency",
  902. &tspi->spi_max_frequency))
  903. tspi->spi_max_frequency = 25000000; /* 25MHz */
  904. }
  905. static const struct tegra_slink_chip_data tegra30_spi_cdata = {
  906. .cs_hold_time = true,
  907. };
  908. static const struct tegra_slink_chip_data tegra20_spi_cdata = {
  909. .cs_hold_time = false,
  910. };
  911. static struct of_device_id tegra_slink_of_match[] = {
  912. { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
  913. { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
  914. {}
  915. };
  916. MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
  917. static int tegra_slink_probe(struct platform_device *pdev)
  918. {
  919. struct spi_master *master;
  920. struct tegra_slink_data *tspi;
  921. struct resource *r;
  922. int ret, spi_irq;
  923. const struct tegra_slink_chip_data *cdata = NULL;
  924. const struct of_device_id *match;
  925. match = of_match_device(tegra_slink_of_match, &pdev->dev);
  926. if (!match) {
  927. dev_err(&pdev->dev, "Error: No device match found\n");
  928. return -ENODEV;
  929. }
  930. cdata = match->data;
  931. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  932. if (!master) {
  933. dev_err(&pdev->dev, "master allocation failed\n");
  934. return -ENOMEM;
  935. }
  936. /* the spi->mode bits understood by this driver: */
  937. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  938. master->setup = tegra_slink_setup;
  939. master->transfer_one_message = tegra_slink_transfer_one_message;
  940. master->auto_runtime_pm = true;
  941. master->num_chipselect = MAX_CHIP_SELECT;
  942. master->bus_num = -1;
  943. platform_set_drvdata(pdev, master);
  944. tspi = spi_master_get_devdata(master);
  945. tspi->master = master;
  946. tspi->dev = &pdev->dev;
  947. tspi->chip_data = cdata;
  948. spin_lock_init(&tspi->lock);
  949. tegra_slink_parse_dt(tspi);
  950. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  951. if (!r) {
  952. dev_err(&pdev->dev, "No IO memory resource\n");
  953. ret = -ENODEV;
  954. goto exit_free_master;
  955. }
  956. tspi->phys = r->start;
  957. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  958. if (IS_ERR(tspi->base)) {
  959. ret = PTR_ERR(tspi->base);
  960. goto exit_free_master;
  961. }
  962. spi_irq = platform_get_irq(pdev, 0);
  963. tspi->irq = spi_irq;
  964. ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  965. tegra_slink_isr_thread, IRQF_ONESHOT,
  966. dev_name(&pdev->dev), tspi);
  967. if (ret < 0) {
  968. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  969. tspi->irq);
  970. goto exit_free_master;
  971. }
  972. tspi->clk = devm_clk_get(&pdev->dev, NULL);
  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_irq;
  977. }
  978. tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  979. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  980. if (tspi->dma_req_sel) {
  981. ret = tegra_slink_init_dma_param(tspi, true);
  982. if (ret < 0) {
  983. dev_err(&pdev->dev, "RxDma Init failed, err %d\n", ret);
  984. goto exit_free_irq;
  985. }
  986. ret = tegra_slink_init_dma_param(tspi, false);
  987. if (ret < 0) {
  988. dev_err(&pdev->dev, "TxDma Init failed, err %d\n", ret);
  989. goto exit_rx_dma_free;
  990. }
  991. tspi->max_buf_size = tspi->dma_buf_size;
  992. init_completion(&tspi->tx_dma_complete);
  993. init_completion(&tspi->rx_dma_complete);
  994. }
  995. init_completion(&tspi->xfer_completion);
  996. pm_runtime_enable(&pdev->dev);
  997. if (!pm_runtime_enabled(&pdev->dev)) {
  998. ret = tegra_slink_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. tspi->def_command_reg = SLINK_M_S;
  1008. tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  1009. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  1010. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  1011. pm_runtime_put(&pdev->dev);
  1012. master->dev.of_node = pdev->dev.of_node;
  1013. ret = devm_spi_register_master(&pdev->dev, master);
  1014. if (ret < 0) {
  1015. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  1016. goto exit_pm_disable;
  1017. }
  1018. return ret;
  1019. exit_pm_disable:
  1020. pm_runtime_disable(&pdev->dev);
  1021. if (!pm_runtime_status_suspended(&pdev->dev))
  1022. tegra_slink_runtime_suspend(&pdev->dev);
  1023. tegra_slink_deinit_dma_param(tspi, false);
  1024. exit_rx_dma_free:
  1025. tegra_slink_deinit_dma_param(tspi, true);
  1026. exit_free_irq:
  1027. free_irq(spi_irq, tspi);
  1028. exit_free_master:
  1029. spi_master_put(master);
  1030. return ret;
  1031. }
  1032. static int tegra_slink_remove(struct platform_device *pdev)
  1033. {
  1034. struct spi_master *master = platform_get_drvdata(pdev);
  1035. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1036. free_irq(tspi->irq, tspi);
  1037. if (tspi->tx_dma_chan)
  1038. tegra_slink_deinit_dma_param(tspi, false);
  1039. if (tspi->rx_dma_chan)
  1040. tegra_slink_deinit_dma_param(tspi, true);
  1041. pm_runtime_disable(&pdev->dev);
  1042. if (!pm_runtime_status_suspended(&pdev->dev))
  1043. tegra_slink_runtime_suspend(&pdev->dev);
  1044. return 0;
  1045. }
  1046. #ifdef CONFIG_PM_SLEEP
  1047. static int tegra_slink_suspend(struct device *dev)
  1048. {
  1049. struct spi_master *master = dev_get_drvdata(dev);
  1050. return spi_master_suspend(master);
  1051. }
  1052. static int tegra_slink_resume(struct device *dev)
  1053. {
  1054. struct spi_master *master = dev_get_drvdata(dev);
  1055. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1056. int ret;
  1057. ret = pm_runtime_get_sync(dev);
  1058. if (ret < 0) {
  1059. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1060. return ret;
  1061. }
  1062. tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  1063. tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  1064. pm_runtime_put(dev);
  1065. return spi_master_resume(master);
  1066. }
  1067. #endif
  1068. static int tegra_slink_runtime_suspend(struct device *dev)
  1069. {
  1070. struct spi_master *master = dev_get_drvdata(dev);
  1071. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1072. /* Flush all write which are in PPSB queue by reading back */
  1073. tegra_slink_readl(tspi, SLINK_MAS_DATA);
  1074. clk_disable_unprepare(tspi->clk);
  1075. return 0;
  1076. }
  1077. static int tegra_slink_runtime_resume(struct device *dev)
  1078. {
  1079. struct spi_master *master = dev_get_drvdata(dev);
  1080. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1081. int ret;
  1082. ret = clk_prepare_enable(tspi->clk);
  1083. if (ret < 0) {
  1084. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1085. return ret;
  1086. }
  1087. return 0;
  1088. }
  1089. static const struct dev_pm_ops slink_pm_ops = {
  1090. SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  1091. tegra_slink_runtime_resume, NULL)
  1092. SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  1093. };
  1094. static struct platform_driver tegra_slink_driver = {
  1095. .driver = {
  1096. .name = "spi-tegra-slink",
  1097. .owner = THIS_MODULE,
  1098. .pm = &slink_pm_ops,
  1099. .of_match_table = tegra_slink_of_match,
  1100. },
  1101. .probe = tegra_slink_probe,
  1102. .remove = tegra_slink_remove,
  1103. };
  1104. module_platform_driver(tegra_slink_driver);
  1105. MODULE_ALIAS("platform:spi-tegra-slink");
  1106. MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  1107. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1108. MODULE_LICENSE("GPL v2");