spi-tegra20-slink.c 36 KB

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