modem_link_device_spi.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067
  1. /*
  2. * Copyright (C) 2011 Google, Inc.
  3. * Copyright (C) 2010 Samsung Electronics.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/irq.h>
  16. #include <linux/gpio.h>
  17. #include <linux/time.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/timer.h>
  20. #include <linux/wakelock.h>
  21. #include <linux/delay.h>
  22. #include <linux/wait.h>
  23. #include <linux/sched.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/if_arp.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/kthread.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_data/modem.h>
  32. #include <linux/sec_param.h>
  33. #include <linux/of_gpio.h>
  34. #include <mach/gpiomux.h>
  35. //#include <linux/platform_data/modem_v2.h>
  36. #include "modem_prj.h"
  37. #include "modem_link_device_spi.h"
  38. #include "modem_utils.h"
  39. #define USE_SPI_HALF_DUPLEX
  40. /* For function which has void parmeter */
  41. static struct spi_link_device *p_spild;
  42. static struct spi_device *p_spi;
  43. static int spi_init_ipc(struct spi_link_device *spild);
  44. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG
  45. void spi_print_data(char *buf, int len)
  46. {
  47. int words = len >> 4;
  48. int residue = len - (words << 4);
  49. int i;
  50. char *b;
  51. char last[80];
  52. char tb[8];
  53. /* Make the last line, if ((len % 16) > 0) */
  54. if (residue > 0) {
  55. memset(last, 0, sizeof(last));
  56. memset(tb, 0, sizeof(tb));
  57. b = buf + (words << 4);
  58. sprintf(last, "%04X: ", (words << 4));
  59. for (i = 0; i < residue; i++) {
  60. sprintf(tb, "%02x ", b[i]);
  61. strcat(last, tb);
  62. if ((i & 0x3) == 0x3) {
  63. sprintf(tb, " ");
  64. strcat(last, tb);
  65. }
  66. }
  67. }
  68. for (i = 0; i < words; i++) {
  69. b = buf + (i << 4);
  70. mif_err("%04X: "
  71. "%02x %02x %02x %02x %02x %02x %02x %02x "
  72. "%02x %02x %02x %02x %02x %02x %02x %02x\n",
  73. (i << 4),
  74. b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
  75. b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]);
  76. }
  77. /* Print the last line */
  78. if (residue > 0)
  79. mif_err("%s\n", last);
  80. }
  81. #endif
  82. static void spi_send_work(int spi_sigs, bool spi_work_t)
  83. {
  84. struct spi_work_type *spi_wq;
  85. spi_wq = kmalloc(sizeof(struct spi_work_type), GFP_ATOMIC);
  86. if (unlikely(!spi_wq)) {
  87. pr_err("[LNK/E] <%s> Failed to kmalloc()\n", __func__);
  88. return;
  89. }
  90. spi_wq->signal_code = spi_sigs;
  91. INIT_WORK(&spi_wq->work, spi_work);
  92. // GSCHO
  93. if (spi_work_t == SPI_WORK_FRONT)
  94. queue_work_front(p_spild->spi_wq, (struct work_struct *)spi_wq);
  95. else
  96. queue_work(p_spild->spi_wq, (struct work_struct *)spi_wq);
  97. }
  98. static irqreturn_t spi_cp_dump_irq_handler(int irq, void *p_ld)
  99. {
  100. struct link_device *ld = (struct link_device *)p_ld;
  101. struct spi_link_device *spild = to_spi_link_device(ld);
  102. irqreturn_t result = IRQ_HANDLED;
  103. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG
  104. pr_info("[SPI] [%s] cp_dump_int irq received. \n",__func__);
  105. #endif
  106. if (!spild->boot_done)
  107. goto exit;
  108. if (!wake_lock_active(&spild->spi_wake_lock)
  109. && spild->send_modem_spi != 1) {
  110. wake_lock(&spild->spi_wake_lock);
  111. pr_debug("[SPI] [%s](%d) spi_wakelock locked . spild->spi_state[%d]\n",
  112. __func__, __LINE__, (int)spild->spi_state);
  113. }
  114. exit:
  115. return result;
  116. }
  117. static irqreturn_t spi_srdy_irq_handler(int irq, void *p_ld)
  118. {
  119. struct link_device *ld = (struct link_device *)p_ld;
  120. struct spi_link_device *spild = to_spi_link_device(ld);
  121. struct io_device *iod;
  122. irqreturn_t result = IRQ_HANDLED;
  123. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG
  124. pr_info("[SPI] [%s](%d) irq received. spild->spi_state[%d]\n",
  125. __func__, __LINE__, (int)spild->spi_state);
  126. #endif
  127. if (!spild->boot_done)
  128. goto exit1;
  129. if (!wake_lock_active(&spild->spi_wake_lock)
  130. && spild->send_modem_spi != 1) {
  131. wake_lock(&spild->spi_wake_lock);
  132. pr_debug("[SPI] [%s](%d) spi_wakelock locked . spild->spi_state[%d]\n",
  133. __func__, __LINE__, (int)spild->spi_state);
  134. }
  135. if (spild->send_modem_spi == 1)
  136. up(&spild->srdy_sem);
  137. /* SRDY interrupt work on SPI_STATE_IDLE state for receive data */
  138. if (spild->spi_state == SPI_STATE_IDLE
  139. || spild->spi_state == SPI_STATE_RX_TERMINATE
  140. || spild->spi_state == SPI_STATE_TX_TERMINATE) {
  141. iod = link_get_iod_with_format(&spild->ld, IPC_FMT);
  142. if (!iod) {
  143. mif_err("no iodevice for modem control\n");
  144. goto exit;
  145. }
  146. if (iod->mc->phone_state != STATE_ONLINE)
  147. goto exit;
  148. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG
  149. pr_err("[SPI] [%s](%d)call spi_send_work spild->spi_state[%d]\n",
  150. __func__, __LINE__, (int)spild->spi_state);
  151. #endif
  152. spi_send_work(SPI_WORK_RECEIVE, SPI_WORK_FRONT);
  153. }
  154. return result;
  155. exit:
  156. if (wake_lock_active(&spild->spi_wake_lock)) {
  157. wake_unlock(&spild->spi_wake_lock);
  158. pr_debug("[SPI] [%s](%d) spi_wakelock unlocked.\n",
  159. __func__, __LINE__);
  160. }
  161. exit1:
  162. return result;
  163. }
  164. static irqreturn_t spi_subsrdy_irq_handler(int irq, void *p_ld)
  165. {
  166. struct link_device *ld = (struct link_device *)p_ld;
  167. struct spi_link_device *spild = to_spi_link_device(ld);
  168. irqreturn_t result = IRQ_HANDLED;
  169. /* SRDY interrupt work on SPI_STATE_TX_WAIT state for send data */
  170. if (spild->spi_state == SPI_STATE_TX_WAIT)
  171. return result;
  172. pr_debug("%s spild->spi_state[%d]\n",
  173. "[SPI] spi_main_subsrdy_rising_handler :",
  174. (int)spild->spi_state);
  175. return result;
  176. }
  177. static int spi_send
  178. (
  179. struct link_device *ld,
  180. struct io_device *iod,
  181. struct sk_buff *skb
  182. )
  183. {
  184. struct sk_buff_head *txq;
  185. enum dev_format fmt = iod->format;
  186. const u32 cmd_ready = 0x12341234;
  187. const u32 cmd_start = 0x45674567;
  188. int ret;
  189. u32 data;
  190. switch (fmt) {
  191. case IPC_FMT:
  192. case IPC_RAW:
  193. case IPC_RFS:
  194. txq = p_spild->skb_txq[fmt];
  195. skb_queue_tail(txq, skb);
  196. ret = skb->len;
  197. break;
  198. case IPC_BOOT:
  199. if (get_user(data, (u32 __user *)skb->data))
  200. return -EFAULT;
  201. if (data == cmd_ready) {
  202. p_spild->ril_send_modem_img = 1;
  203. p_spild->ril_send_cnt = 0;
  204. } else if (data == cmd_start) {
  205. p_spild->ril_send_modem_img = 0;
  206. if (!queue_work(p_spild->spi_modem_wq,
  207. &p_spild->send_modem_w))
  208. pr_err("(%d) already exist w-q\n",
  209. __LINE__);
  210. } else {
  211. if (p_spild->ril_send_modem_img) {
  212. memcpy((void *)(p_spild->p_virtual_buff +
  213. p_spild->ril_send_cnt),
  214. skb->data, skb->len);
  215. p_spild->ril_send_cnt += skb->len;
  216. }
  217. }
  218. ret = skb->len;
  219. dev_kfree_skb_any(skb);
  220. return ret;
  221. default:
  222. pr_err("[LNK/E] <%s:%s> No TXQ for %s\n",
  223. __func__, ld->name, iod->name);
  224. dev_kfree_skb_any(skb);
  225. return 0;
  226. }
  227. spi_send_work(SPI_WORK_SEND, SPI_WORK);
  228. return ret;
  229. }
  230. //SPI_SETUP
  231. static int spi_check_cp_bin(struct spi_link_device *spild)
  232. {
  233. /* need to dowload : 1, don't need : 0 */
  234. int retval = 0;
  235. sec_get_param(param_update_cp_bin, (void*)&retval);
  236. return retval;
  237. }
  238. static int spi_updated_cp_bin(struct spi_link_device *spild)
  239. {
  240. /* don't need to download ZZ: 0 */
  241. int retval = 0, updated_cp_bin=0;
  242. sec_set_param(param_update_cp_bin, (void*)&updated_cp_bin);
  243. return retval;
  244. }
  245. static int spi_ioctl(struct link_device *ld, struct io_device *iod,
  246. unsigned int cmd, unsigned long arg)
  247. {
  248. struct spi_link_device *spild = to_spi_link_device(ld);
  249. int err = 0;
  250. mif_debug("%s: spi_ioctl cmd 0x%08X\n", ld->name, cmd);
  251. switch (cmd) {
  252. case IOCTL_DPRAM_CHECK_CP_BIN:
  253. //modified. dklee
  254. spi_init_ipc(p_spild);
  255. err = spi_check_cp_bin(spild);
  256. mif_debug("%s: check_cp_bin [%d]\n", ld->name, err);
  257. break;
  258. case IOCTL_DPRAM_UPDATED_CP_BIN:
  259. err = spi_updated_cp_bin(spild);
  260. mif_debug("%s: check_cp_bin [%d]\n", ld->name, err);
  261. break;
  262. default:
  263. pr_err("%s: ERR! invalid cmd 0x%08X\n", ld->name, cmd);
  264. err = -EINVAL;
  265. break;
  266. }
  267. return err;
  268. }
  269. static int spi_register_isr
  270. (
  271. unsigned irq,
  272. irqreturn_t (*isr)(int, void*),
  273. unsigned long flag,
  274. const char *name,
  275. struct link_device *ld
  276. )
  277. {
  278. int ret = 0;
  279. ret = request_irq(irq, isr, flag, name, ld);
  280. if (ret) {
  281. pr_err("[LNK/E] <%s> request_irq fail (%d)\n",
  282. __func__, ret);
  283. goto err;
  284. }
  285. if (irq == gpio_to_irq(p_spild->gpio_cp_dump_int)){
  286. ret = enable_irq_wake(irq);
  287. if (ret) {
  288. pr_err("[LNK/E] <%s> enable_irq_wake fail (%d)\n",
  289. __func__, ret);
  290. free_irq(irq, ld);
  291. goto err;
  292. }
  293. }
  294. pr_debug("[LNK] <%s> IRQ#%d handler is registered.\n", __func__, irq);
  295. err:
  296. return ret;
  297. }
  298. void spi_unregister_isr(unsigned irq, void *data)
  299. {
  300. free_irq(irq, data);
  301. }
  302. int spi_tx_rx_sync(u8 *tx_d, u8 *rx_d, unsigned len)
  303. {
  304. struct spi_transfer t;
  305. struct spi_message msg;
  306. memset(&t, 0, sizeof t);
  307. t.len = len;
  308. t.tx_buf = tx_d;
  309. t.rx_buf = rx_d;
  310. t.cs_change = 1;
  311. t.bits_per_word = 32;
  312. //t.bits_per_word = 8;
  313. // t.speed_hz = 10800000;
  314. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG
  315. printk("%s : len :- %d\n", __func__, len);
  316. printk("%s : mode :- %d\n", __func__, p_spi->mode);
  317. #endif
  318. spi_message_init(&msg);
  319. spi_message_add_tail(&t, &msg);
  320. return spi_sync(p_spi, &msg);
  321. }
  322. static int spi_buff_write
  323. (
  324. struct spi_link_device *spild,
  325. int dev_id,
  326. const char *buff,
  327. unsigned size
  328. )
  329. {
  330. u32 templength, buf_length;
  331. u32 spi_data_mux;
  332. u32 spi_packet_free_length;
  333. u8 *spi_packet;
  334. struct spi_data_packet_header *spi_packet_header;
  335. spi_packet_header = (struct spi_data_packet_header *)spild->buff;
  336. spi_packet = (char *)spild->buff;
  337. spi_packet_free_length = SPI_DATA_PACKET_MAX_PACKET_BODY_SIZE -
  338. spi_packet_header->current_data_size;
  339. buf_length = size + SPI_DATA_MUX_SIZE + SPI_DATA_LENGTH_SIZE;
  340. /* not enough space in spi packet */
  341. if (spi_packet_free_length < buf_length) {
  342. spi_packet_header->more = 1;
  343. return 0;
  344. }
  345. /* check spi mux type */
  346. switch (dev_id) {
  347. case IPC_FMT:
  348. spi_data_mux = SPI_DATA_MUX_IPC;
  349. break;
  350. case IPC_RAW:
  351. spi_data_mux = SPI_DATA_MUX_RAW;
  352. break;
  353. case IPC_RFS:
  354. spi_data_mux = SPI_DATA_MUX_RFS;
  355. break;
  356. default:
  357. pr_err("%s %s\n",
  358. "[SPI] ERROR : spi_buff_write:",
  359. "invalid dev_id");
  360. return 0;
  361. }
  362. /* copy spi mux field */
  363. memcpy(spi_packet + SPI_DATA_PACKET_HEADER_SIZE +
  364. spi_packet_header->current_data_size,
  365. &spi_data_mux, SPI_DATA_MUX_SIZE);
  366. spi_packet_header->current_data_size += SPI_DATA_MUX_SIZE;
  367. /* copy spi data length field */
  368. templength = size-SPI_DATA_BOF_SIZE-SPI_DATA_EOF_SIZE;
  369. memcpy(spi_packet + SPI_DATA_PACKET_HEADER_SIZE +
  370. spi_packet_header->current_data_size,
  371. &templength, SPI_DATA_LENGTH_SIZE);
  372. spi_packet_header->current_data_size += SPI_DATA_LENGTH_SIZE;
  373. /* copy data field */
  374. memcpy(spi_packet + SPI_DATA_PACKET_HEADER_SIZE +
  375. spi_packet_header->current_data_size,
  376. buff, size);
  377. spi_packet_header->current_data_size += size;
  378. return buf_length;
  379. }
  380. static void spi_prepare_tx_packet(void)
  381. {
  382. struct sk_buff *skb;
  383. int ret;
  384. int i;
  385. for (i = 0; i < p_spild->max_ipc_dev; i++) {
  386. while ((skb = skb_dequeue(p_spild->skb_txq[i]))) {
  387. ret = spi_buff_write(p_spild, i, skb->data, skb->len);
  388. if (!ret) {
  389. skb_queue_head(p_spild->skb_txq[i], skb);
  390. break;
  391. }
  392. dev_kfree_skb_any(skb);
  393. }
  394. }
  395. }
  396. static void spi_start_data_send(void)
  397. {
  398. int i;
  399. for (i = 0; i < p_spild->max_ipc_dev; i++) {
  400. if (skb_queue_len(p_spild->skb_txq[i]) > 0)
  401. spi_send_work(SPI_WORK_SEND, SPI_WORK);
  402. }
  403. }
  404. static void spi_tx_work(void)
  405. {
  406. struct spi_link_device *spild;
  407. struct io_device *iod;
  408. char *spi_packet_buf;
  409. char *spi_sync_buf;
  410. spild = p_spild;
  411. iod = link_get_iod_with_format(&spild->ld, IPC_FMT);
  412. if (!iod) {
  413. mif_err("no iodevice for modem control\n");
  414. return;
  415. }
  416. if (iod->mc->phone_state != STATE_ONLINE)
  417. return;
  418. /* check SUB SRDY, SRDY state */
  419. if (gpio_get_value(spild->gpio_ipc_sub_srdy) ==
  420. SPI_GPIOLEVEL_HIGH ||
  421. gpio_get_value(spild->gpio_ipc_srdy) ==
  422. SPI_GPIOLEVEL_HIGH) {
  423. spi_start_data_send();
  424. return;
  425. }
  426. // GSCHO
  427. if (get_console_suspended())
  428. return;
  429. if (spild->spi_state == SPI_STATE_END)
  430. return;
  431. /* change state SPI_STATE_IDLE to SPI_STATE_TX_WAIT */
  432. spild->spi_state = SPI_STATE_TX_WAIT;
  433. spild->spi_timer_tx_state = SPI_STATE_TIME_START;
  434. gpio_direction_output(spild->gpio_ipc_mrdy, SPI_GPIOLEVEL_HIGH);
  435. /* Start TX timer */
  436. spild->spi_tx_timer.expires = jiffies +
  437. ((SPI_TIMER_TX_WAIT_TIME * HZ) / 1000);
  438. add_timer(&spild->spi_tx_timer);
  439. /* check SUBSRDY state */
  440. while (gpio_get_value(spild->gpio_ipc_sub_srdy) ==
  441. SPI_GPIOLEVEL_LOW) {
  442. if (spild->spi_timer_tx_state == SPI_STATE_TIME_OVER) {
  443. pr_err("%s spild->spi_state=[%d]\n",
  444. "[spi_tx_work] == spi Fail to receiving SUBSRDY CONF :",
  445. (int)spild->spi_state);
  446. spild->spi_timer_tx_state = SPI_STATE_TIME_START;
  447. gpio_direction_output(spild->gpio_ipc_mrdy,
  448. SPI_GPIOLEVEL_LOW);
  449. /* change state SPI_STATE_TX_WAIT */
  450. /* to SPI_STATE_IDLE */
  451. spild->spi_state = SPI_STATE_IDLE;
  452. spi_send_work(SPI_WORK_SEND, SPI_WORK);
  453. return;
  454. }
  455. }
  456. /* Stop TX timer */
  457. del_timer(&spild->spi_tx_timer);
  458. if (spild->spi_state != SPI_STATE_START
  459. && spild->spi_state != SPI_STATE_END
  460. && spild->spi_state != SPI_STATE_INIT) {
  461. spi_packet_buf = spild->buff;
  462. spi_sync_buf = spild->sync_buff;
  463. gpio_direction_output(spild->gpio_ipc_sub_mrdy, SPI_GPIOLEVEL_HIGH);
  464. gpio_direction_output(spild->gpio_ipc_mrdy, SPI_GPIOLEVEL_LOW);
  465. /* change state SPI_MAIN_STATE_TX_SENDING */
  466. spild->spi_state = SPI_STATE_TX_SENDING;
  467. memset(spi_packet_buf, 0, SPI_MAX_PACKET_SIZE);
  468. memset(spi_sync_buf, 0, SPI_MAX_PACKET_SIZE);
  469. spi_prepare_tx_packet();
  470. #ifdef USE_SPI_HALF_DUPLEX
  471. if (spi_tx_rx_sync((void *)spi_packet_buf, (void *)NULL,
  472. SPI_MAX_PACKET_SIZE)) {
  473. #else
  474. if (spi_tx_rx_sync((void *)spi_packet_buf, (void *)spi_sync_buf,
  475. SPI_MAX_PACKET_SIZE)) {
  476. #endif
  477. /* TODO: save failed packet */
  478. /* back data to each queue */
  479. pr_err("[SPI] spi_dev_send fail\n");
  480. /* add cp reset when spi sync fail */
  481. if (iod)
  482. iod->modem_state_changed(iod,
  483. STATE_CRASH_RESET);
  484. }
  485. spild->spi_state = SPI_STATE_TX_TERMINATE;
  486. gpio_direction_output(spild->gpio_ipc_sub_mrdy, SPI_GPIOLEVEL_LOW);
  487. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG
  488. pr_info("[SPI] spi_tx_work : success - spi_state=[%d]\n",
  489. (int)spild->spi_state);
  490. #endif
  491. /* change state SPI_MAIN_STATE_TX_SENDING to SPI_STATE_IDLE */
  492. spild->spi_state = SPI_STATE_IDLE;
  493. spi_start_data_send();
  494. } else
  495. return;
  496. }
  497. int spi_buff_read(struct spi_link_device *spild)
  498. {
  499. struct link_device *ld;
  500. struct spi_data_packet_header *spi_packet_header;
  501. struct sk_buff *skb;
  502. char *spi_packet;
  503. int dev_id;
  504. unsigned int spi_packet_length;
  505. unsigned int spi_packet_cur_pos = SPI_DATA_PACKET_HEADER_SIZE;
  506. unsigned int spi_data_mux;
  507. unsigned int spi_data_length;
  508. unsigned int data_length;
  509. u8 *spi_cur_data;
  510. u8 *dst;
  511. spi_packet = spild->buff;
  512. ld = &spild->ld;
  513. /* check spi packet header */
  514. if (*(unsigned int *)spi_packet == 0x00000000
  515. || *(unsigned int *)spi_packet == 0xFFFFFFFF) {
  516. /* if spi header is invalid, */
  517. /* read spi header again with next 4 byte */
  518. spi_packet += SPI_DATA_PACKET_HEADER_SIZE;
  519. }
  520. /* read spi packet header */
  521. spi_packet_header = (struct spi_data_packet_header *)spi_packet;
  522. spi_packet_length = SPI_DATA_PACKET_HEADER_SIZE +
  523. spi_packet_header->current_data_size;
  524. do {
  525. /* read spi data mux and set current queue */
  526. memcpy(&spi_data_mux,
  527. spi_packet + spi_packet_cur_pos, SPI_DATA_MUX_SIZE);
  528. switch (spi_data_mux & SPI_DATA_MUX_NORMAL_MASK) {
  529. case SPI_DATA_MUX_IPC:
  530. dev_id = IPC_FMT;
  531. break;
  532. case SPI_DATA_MUX_RAW:
  533. dev_id = IPC_RAW;
  534. break;
  535. case SPI_DATA_MUX_RFS:
  536. dev_id = IPC_RFS;
  537. break;
  538. default:
  539. pr_err("%s len[%u], pos[%u]\n",
  540. "[SPI] ERROR : spi_buff_read : MUX error",
  541. spi_packet_length, spi_packet_cur_pos);
  542. return spi_packet_cur_pos - SPI_DATA_PACKET_HEADER_SIZE;
  543. }
  544. /* read spi data length */
  545. memcpy(&spi_data_length, spi_packet +
  546. spi_packet_cur_pos + SPI_DATA_LENGTH_OFFSET,
  547. SPI_DATA_LENGTH_SIZE);
  548. data_length = spi_data_length +
  549. SPI_DATA_BOF_SIZE + SPI_DATA_EOF_SIZE;
  550. spi_data_length += SPI_DATA_HEADER_SIZE;
  551. /* read data and make spi data */
  552. spi_cur_data = spi_packet + spi_packet_cur_pos;
  553. /* enqueue spi data */
  554. skb = alloc_skb(data_length, GFP_KERNEL);
  555. if (unlikely(!skb)) {
  556. pr_err("%s %s\n",
  557. "[SPI] ERROR : spi_buff_read:",
  558. "Can't allocate memory for SPI");
  559. return -ENOMEM;
  560. }
  561. dst = skb_put(skb, data_length);
  562. memcpy(dst, spi_packet +
  563. spi_packet_cur_pos + SPI_DATA_BOF_OFFSET,
  564. data_length);
  565. skb_queue_tail(&spild->skb_rxq[dev_id], skb);
  566. /* move spi packet current posision */
  567. spi_packet_cur_pos += spi_data_length;
  568. } while ((spi_packet_length - 1) > spi_packet_cur_pos);
  569. return 1;
  570. }
  571. static void spi_rx_work(void)
  572. {
  573. struct link_device *ld;
  574. struct spi_link_device *spild;
  575. struct sk_buff *skb;
  576. struct io_device *iod;
  577. char *spi_packet_buf;
  578. char *spi_sync_buf;
  579. int i;
  580. #if 0 //dklee temp
  581. char test_spi_send_buff[256];
  582. int k;
  583. #endif
  584. spild = p_spild;
  585. ld = &spild->ld;
  586. if (!spild)
  587. pr_err("[LNK/E] <%s> dpld == NULL\n", __func__);
  588. iod = link_get_iod_with_format(&spild->ld, IPC_FMT);
  589. if (!iod) {
  590. mif_err("no iodevice for modem control\n");
  591. return;
  592. }
  593. if (iod->mc->phone_state != STATE_ONLINE)
  594. return;
  595. if (!wake_lock_active(&spild->spi_wake_lock) ||
  596. gpio_get_value(spild->gpio_ipc_srdy) == SPI_GPIOLEVEL_LOW ||
  597. //G9009D incoming call screen delay issue
  598. //get_console_suspended() ||
  599. spild->spi_state == SPI_STATE_END)
  600. return;
  601. spild->spi_state = SPI_STATE_RX_WAIT;
  602. spild->spi_timer_rx_state = SPI_STATE_TIME_START;
  603. #if 1 //temp set gpio ourput enable
  604. //modem may set this pin as input mode . fix it later.
  605. gpio_direction_output(spild->gpio_ipc_sub_mrdy, SPI_GPIOLEVEL_HIGH);
  606. #else
  607. gpio_set_value(spild->gpio_ipc_sub_mrdy, SPI_GPIOLEVEL_HIGH);
  608. #endif
  609. /* Start TX timer */
  610. spild->spi_rx_timer.expires = jiffies +
  611. ((SPI_TIMER_RX_WAIT_TIME * HZ) / 1000);
  612. add_timer(&spild->spi_rx_timer);
  613. /* check SUBSRDY state */
  614. while (gpio_get_value(spild->gpio_ipc_sub_srdy) ==
  615. SPI_GPIOLEVEL_LOW) {
  616. if (spild->spi_timer_rx_state == SPI_STATE_TIME_OVER) {
  617. pr_err("[SPI] ERROR(Failed MASTER RX:%d ms)",
  618. SPI_TIMER_RX_WAIT_TIME);
  619. spild->spi_timer_rx_state = SPI_STATE_TIME_START;
  620. gpio_direction_output(spild->gpio_ipc_sub_mrdy,
  621. SPI_GPIOLEVEL_LOW);
  622. /* change state SPI_MAIN_STATE_RX_WAIT */
  623. /* to SPI_STATE_IDLE */
  624. spild->spi_state = SPI_STATE_IDLE;
  625. return;
  626. }
  627. }
  628. /* Stop TX timer */
  629. del_timer(&spild->spi_rx_timer);
  630. if (spild->spi_state == SPI_STATE_START
  631. || spild->spi_state == SPI_STATE_END
  632. || spild->spi_state == SPI_STATE_INIT)
  633. return;
  634. spi_packet_buf = spild->buff;
  635. spi_sync_buf = spild->sync_buff;
  636. memset(spi_packet_buf, 0, SPI_MAX_PACKET_SIZE);
  637. memset(spi_sync_buf, 0, SPI_MAX_PACKET_SIZE);
  638. #if 0 //DKLee test tem log
  639. for(k=0; k<256; k++)
  640. test_spi_send_buff[k]=k;
  641. mif_err("Data sent\n");
  642. spi_print_data(test_spi_send_buff, 64);
  643. #endif
  644. #if 1 //test dklee
  645. #ifdef USE_SPI_HALF_DUPLEX
  646. if (spi_tx_rx_sync((void *)NULL, (void *)spi_packet_buf,
  647. SPI_MAX_PACKET_SIZE) == 0) {
  648. #else
  649. if (spi_tx_rx_sync((void *)spi_sync_buf, (void *)spi_packet_buf,
  650. SPI_MAX_PACKET_SIZE) == 0) {
  651. #endif
  652. #else
  653. if (spi_tx_rx_sync((void *)test_spi_send_buff, (void *)spi_packet_buf,
  654. SPI_MAX_PACKET_SIZE) == 0) {
  655. #endif
  656. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG //DKLee test tem log
  657. mif_err("Data from CP\n");
  658. spi_print_data(spi_packet_buf, 64);
  659. #endif
  660. /* parsing SPI packet */
  661. if (spi_buff_read(spild) > 0) {
  662. /* call function for send data to IPC, RAW, RFS */
  663. for (i = 0; i < spild->max_ipc_dev; i++) {
  664. iod = spild->iod[i];
  665. while ((skb = skb_dequeue(&spild->skb_rxq[i]))
  666. != NULL) {
  667. if (iod->recv(iod, ld, skb->data,
  668. skb->len) < 0)
  669. pr_err("[LNK/E] <%s:%s> recv fail\n",
  670. __func__, ld->name);
  671. dev_kfree_skb_any(skb);
  672. }
  673. }
  674. }
  675. } else {
  676. pr_err("%s %s\n",
  677. "[SPI] ERROR : spi_rx_work :",
  678. "spi sync failed");
  679. /* add cp reset when spi sync fail */
  680. if (iod)
  681. iod->modem_state_changed(iod,
  682. STATE_CRASH_RESET);
  683. }
  684. spild->spi_state = SPI_STATE_RX_TERMINATE;
  685. gpio_direction_output(spild->gpio_ipc_sub_mrdy, SPI_GPIOLEVEL_LOW);
  686. /* change state SPI_MAIN_STATE_RX_WAIT to SPI_STATE_IDLE */
  687. spild->spi_state = SPI_STATE_IDLE;
  688. spi_start_data_send();
  689. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG //DKLee test tem log
  690. mif_err("Data from CP END\n");
  691. spi_print_data(spi_packet_buf, 64);
  692. #endif
  693. }
  694. static int spi_init_ipc(struct spi_link_device *spild)
  695. {
  696. struct link_device *ld = &spild->ld;
  697. int i;
  698. /* Make aliases to each IO device */
  699. for (i = 0; i < MAX_DEV_FORMAT; i++)
  700. spild->iod[i] = link_get_iod_with_format(ld, i);
  701. spild->iod[IPC_RAW] = spild->iod[IPC_MULTI_RAW];
  702. /* List up the IO devices connected to each IPC channel */
  703. for (i = 0; i < MAX_DEV_FORMAT; i++) {
  704. if (spild->iod[i])
  705. pr_err("[LNK] <%s:%s> spild->iod[%d]->name = %s\n",
  706. __func__, ld->name, i, spild->iod[i]->name);
  707. else
  708. pr_err("[LNK] <%s:%s> No spild->iod[%d]\n",
  709. __func__, ld->name, i);
  710. }
  711. return 0;
  712. }
  713. unsigned int sprd_crc_calc(char *buf_ptr, unsigned int len)
  714. {
  715. unsigned int i;
  716. unsigned short crc = 0;
  717. while (len-- != 0) {
  718. for (i = 0x80; i != 0 ; i = i>>1) {
  719. if ((crc & 0x8000) != 0) {
  720. crc = crc << 1 ;
  721. crc = crc ^ 0x1021;
  722. } else {
  723. crc = crc << 1 ;
  724. }
  725. if ((*buf_ptr & i) != 0)
  726. crc = crc ^ 0x1021;
  727. }
  728. buf_ptr++;
  729. }
  730. return crc;
  731. }
  732. unsigned short sprd_crc_calc_fdl(unsigned short *src, int len)
  733. {
  734. unsigned int sum = 0;
  735. unsigned short SourceValue, DestValue = 0;
  736. unsigned short lowSourceValue, hiSourceValue = 0;
  737. /* Get sum value of the source.*/
  738. while (len > 1) {
  739. SourceValue = *src++;
  740. DestValue = 0;
  741. lowSourceValue = (SourceValue & 0xFF00) >> 8;
  742. hiSourceValue = (SourceValue & 0x00FF) << 8;
  743. DestValue = lowSourceValue | hiSourceValue;
  744. sum += DestValue;
  745. len -= 2;
  746. }
  747. if (len == 1)
  748. sum += *((unsigned char *) src);
  749. sum = (sum >> 16) + (sum & 0x0FFFF);
  750. sum += (sum >> 16);
  751. return ~sum;
  752. }
  753. int encode_msg(struct sprd_image_buf *img, int bcrc)
  754. {
  755. u16 crc; /* CRC value*/
  756. u8 *src_ptr; /* source buffer pointer*/
  757. int dest_len; /* output buffer length*/
  758. u8 *dest_ptr; /* dest buffer pointer*/
  759. u8 high_crc, low_crc = 0;
  760. register int curr;
  761. /* CRC Check. */
  762. src_ptr = img->tx_b;
  763. /* CRC Check. */
  764. if (bcrc)
  765. crc = sprd_crc_calc(src_ptr, img->tx_size);
  766. else
  767. crc = sprd_crc_calc_fdl
  768. ((unsigned short *)src_ptr, img->tx_size);
  769. high_crc = (crc>>8) & 0xFF;
  770. low_crc = crc & 0xFF;
  771. /* Get the total size to be allocated.*/
  772. dest_len = 0;
  773. for (curr = 0; curr < img->tx_size; curr++) {
  774. switch (*(src_ptr+curr)) {
  775. case HDLC_FLAG:
  776. case HDLC_ESCAPE:
  777. dest_len += 2;
  778. break;
  779. default:
  780. dest_len++;
  781. break;
  782. }
  783. }
  784. switch (low_crc) {
  785. case HDLC_FLAG:
  786. case HDLC_ESCAPE:
  787. dest_len += 2;
  788. break;
  789. default:
  790. dest_len++;
  791. }
  792. switch (high_crc) {
  793. case HDLC_FLAG:
  794. case HDLC_ESCAPE:
  795. dest_len += 2;
  796. break;
  797. default:
  798. dest_len++;
  799. }
  800. dest_ptr = kmalloc(dest_len + 2, GFP_KERNEL);
  801. /* Memory Allocate fail.*/
  802. if (dest_ptr == NULL)
  803. return -ENOMEM;
  804. *dest_ptr = HDLC_FLAG;
  805. dest_len = 1;
  806. /* do escape*/
  807. for (curr = 0; curr < img->tx_size; curr++) {
  808. switch (*(src_ptr+curr)) {
  809. case HDLC_FLAG:
  810. case HDLC_ESCAPE:
  811. *(dest_ptr + dest_len++) = HDLC_ESCAPE;
  812. *(dest_ptr + dest_len++) =
  813. *(src_ptr + curr) ^ HDLC_ESCAPE_MASK;
  814. break;
  815. default:
  816. *(dest_ptr + dest_len++) = *(src_ptr + curr);
  817. break;
  818. }
  819. }
  820. switch (high_crc) {
  821. case HDLC_FLAG:
  822. case HDLC_ESCAPE:
  823. *(dest_ptr + dest_len++) = HDLC_ESCAPE;
  824. *(dest_ptr + dest_len++) = high_crc ^ HDLC_ESCAPE_MASK;
  825. break;
  826. default:
  827. *(dest_ptr + dest_len++) = high_crc;
  828. }
  829. switch (low_crc) {
  830. case HDLC_FLAG:
  831. case HDLC_ESCAPE:
  832. *(dest_ptr + dest_len++) = HDLC_ESCAPE;
  833. *(dest_ptr + dest_len++) = low_crc ^ HDLC_ESCAPE_MASK;
  834. break;
  835. default:
  836. *(dest_ptr + dest_len++) = low_crc;
  837. }
  838. *(dest_ptr + dest_len++) = HDLC_FLAG;
  839. memcpy(img->encoded_tx_b, dest_ptr, dest_len);
  840. img->encoded_tx_size = dest_len;
  841. kfree(dest_ptr);
  842. return 0;
  843. }
  844. int decode_msg(struct sprd_image_buf *img, int bcrc)
  845. {
  846. u16 crc; /* CRC value*/
  847. u8 *src_ptr; /* source buffer pointer*/
  848. int dest_len; /* output buffer length*/
  849. u8 *dest_ptr; /* dest buffer pointer*/
  850. register int curr;
  851. /* Check if exist End Flag.*/
  852. src_ptr = img->rx_b;
  853. dest_len = 0;
  854. if (img->rx_size < 4)
  855. return -EINVAL;
  856. /* Get the total size to be allocated for decoded message.*/
  857. for (curr = 1; curr < img->rx_size - 1; curr++) {
  858. switch (*(src_ptr + curr)) {
  859. case HDLC_ESCAPE:
  860. curr++;
  861. dest_len++;
  862. break;
  863. default:
  864. dest_len++;
  865. break;
  866. }
  867. }
  868. /* Allocate meomory for decoded message*/
  869. dest_ptr = kmalloc(dest_len, GFP_KERNEL);
  870. /* Memory allocate fail.*/
  871. if (dest_ptr == NULL)
  872. return -ENOMEM;
  873. memset(dest_ptr, 0, dest_len);
  874. curr = 0;
  875. dest_len = 0;
  876. /* Do de-escape.*/
  877. for (curr = 1; curr < img->rx_size - 1; curr++) {
  878. switch (*(src_ptr + curr)) {
  879. case HDLC_ESCAPE:
  880. curr++;
  881. *(dest_ptr + dest_len) =
  882. *(src_ptr + curr) ^ HDLC_ESCAPE_MASK;
  883. break;
  884. default:
  885. *(dest_ptr + dest_len) = *(src_ptr + curr);
  886. break;
  887. }
  888. dest_len = dest_len + 1;
  889. }
  890. /* CRC Check. */
  891. if (bcrc)
  892. crc = sprd_crc_calc(dest_ptr, dest_len);
  893. else
  894. crc = sprd_crc_calc_fdl((unsigned short *)dest_ptr, dest_len);
  895. if (crc != CRC_16_L_OK) {
  896. pr_err("CRC error : 0x%X", crc);
  897. kfree(dest_ptr);
  898. return -EPERM;
  899. }
  900. memcpy(img->decoded_rx_b, dest_ptr, dest_len - CRC_CHECK_SIZE);
  901. img->decoded_rx_size = dest_len - CRC_CHECK_SIZE ;
  902. kfree(dest_ptr);
  903. return 0;
  904. }
  905. static int if_spi_send_modem_bin_execute_cmd
  906. (u8 *spi_ptr, u32 spi_size, u16 spi_type,
  907. u16 spi_crc, struct sprd_image_buf *sprd_img)
  908. {
  909. int i, retval;
  910. u16 send_packet_size;
  911. u8 *send_packet_data;
  912. u16 d1_crc;
  913. u16 d2_crc = spi_crc;
  914. u16 type = spi_type;
  915. //dklee test add log
  916. static u16 data_sent_count = 0;
  917. static u16 err_count = 0;
  918. /* D1 */
  919. send_packet_size = spi_size; /* u32 -> u16 */
  920. sprd_img->tx_size = 6;
  921. M_16_SWAP(d2_crc);
  922. memcpy(sprd_img->tx_b, &send_packet_size, sizeof(send_packet_size));
  923. memcpy((sprd_img->tx_b+2), &type, sizeof(type));
  924. memcpy((sprd_img->tx_b+4), &d2_crc, sizeof(d2_crc));
  925. d1_crc = sprd_crc_calc_fdl
  926. ((unsigned short *)sprd_img->tx_b, sprd_img->tx_size);
  927. M_16_SWAP(d1_crc);
  928. memcpy((sprd_img->tx_b+6), &d1_crc, sizeof(d1_crc));
  929. sprd_img->tx_size += 2;
  930. if (down_timeout(&p_spild->srdy_sem, 2 * HZ)) {
  931. pr_err("(%d) SRDY TimeOUT!!! SRDY : %d, SEM : %d\n",
  932. __LINE__, gpio_get_value(p_spild->gpio_modem_bin_srdy),
  933. p_spild->srdy_sem.count);
  934. goto err;
  935. }
  936. #if 0//def USE_SPI_HALF_DUPLEX
  937. retval = spi_tx_rx_sync
  938. (sprd_img->tx_b, NULL , sprd_img->tx_size);
  939. #else
  940. retval = spi_tx_rx_sync
  941. (sprd_img->tx_b, sprd_img->rx_b, sprd_img->tx_size);
  942. #endif
  943. if (retval != 0) {
  944. pr_err("(%d) spi sync error : %d\n",
  945. __LINE__, retval);
  946. goto err;
  947. }
  948. if ((type == 0x0003) || (type == 0x0004)) {
  949. pr_err("D2 Skip!!\n");
  950. goto ACK;
  951. }
  952. /* D2 */
  953. send_packet_data = spi_ptr;
  954. if (down_timeout(&p_spild->srdy_sem, 2 * HZ)) {
  955. pr_err("(%d) SRDY TimeOUT!!! SRDY : %d, SEM : %d\n",
  956. __LINE__, gpio_get_value(p_spild->gpio_modem_bin_srdy),
  957. p_spild->srdy_sem.count);
  958. goto err;
  959. }
  960. data_sent_count++;
  961. #if 0//def USE_SPI_HALF_DUPLEX
  962. retval = spi_tx_rx_sync
  963. (send_packet_data, NULL , send_packet_size);
  964. #else
  965. retval = spi_tx_rx_sync
  966. (send_packet_data, sprd_img->rx_b, send_packet_size);
  967. #endif
  968. if (retval != 0) {
  969. pr_err("(%d) spi sync error : %d\n",
  970. __LINE__, retval);
  971. goto err;
  972. }
  973. ACK:
  974. if (p_spild->is_cp_reset) {
  975. while (!gpio_get_value(p_spild->gpio_modem_bin_srdy))
  976. ;
  977. } else {
  978. if (down_timeout(&p_spild->srdy_sem, 2 * HZ)) {
  979. pr_err("(%d) SRDY TimeOUT!!! SRDY : %d, SEM : %d\n",
  980. __LINE__, gpio_get_value(p_spild->gpio_modem_bin_srdy),
  981. p_spild->srdy_sem.count);
  982. pr_err("[SPI DUMP] TX_D1(%d) : [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
  983. sprd_img->tx_size, sprd_img->tx_b[0], sprd_img->tx_b[1],
  984. sprd_img->tx_b[2], sprd_img->tx_b[3], sprd_img->tx_b[4],
  985. sprd_img->tx_b[5], sprd_img->tx_b[6],
  986. sprd_img->tx_b[7]);
  987. pr_err("[SPI DUMP] TX_D2(%d) : [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
  988. send_packet_size, spi_ptr[0], spi_ptr[1], spi_ptr[2],
  989. spi_ptr[3], spi_ptr[4], spi_ptr[5], spi_ptr[6],
  990. spi_ptr[7]);
  991. /* WA (CP Reset) jongmoon.suh */
  992. if (gpio_get_value(p_spild->gpio_modem_bin_srdy))
  993. ;
  994. else
  995. goto err;
  996. }
  997. }
  998. #if 0//def USE_SPI_HALF_DUPLEX
  999. retval = spi_tx_rx_sync(NULL, sprd_img->rx_b, 8);
  1000. #else
  1001. memset(sprd_img->tx_b, 0, SPRD_BLOCK_SIZE+10);
  1002. retval = spi_tx_rx_sync(sprd_img->tx_b, sprd_img->rx_b, 8);
  1003. #endif
  1004. if (retval != 0) {
  1005. pr_err("(%d) spi sync error : %d\n",
  1006. __LINE__, retval);
  1007. goto err;
  1008. }
  1009. memcpy(sprd_img->decoded_rx_b, sprd_img->rx_b, 4);
  1010. if ((*(sprd_img->decoded_rx_b+0) == 0x00) && \
  1011. (*(sprd_img->decoded_rx_b+1) == 0x80) && \
  1012. (*(sprd_img->decoded_rx_b+2) == 0x00) && \
  1013. (*(sprd_img->decoded_rx_b+3) == 0x00)) {
  1014. // pr_err("[SPRD] CP sent ACK");
  1015. } else {
  1016. pr_err("Transfer ACK error! srdy_sem = %d err count [%d]\n",
  1017. p_spild->srdy_sem.count, ++err_count);
  1018. pr_err("sent count = [%d]\n", data_sent_count);
  1019. pr_err("[SPI DUMP] RX(%d) : [ ", sprd_img->rx_size);
  1020. for (i = 0; i < 15; i++)
  1021. pr_err("%02x ", *((u8 *)(sprd_img->rx_b + i)));
  1022. pr_err("]");
  1023. pr_err("[SPI DUMP] TX_D1(%d) : [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
  1024. sprd_img->tx_size, sprd_img->tx_b[0], sprd_img->tx_b[1],
  1025. sprd_img->tx_b[2], sprd_img->tx_b[3], sprd_img->tx_b[4],
  1026. sprd_img->tx_b[5], sprd_img->tx_b[6],
  1027. sprd_img->tx_b[7]);
  1028. pr_err("[SPI DUMP] TX_D2(%d) : [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
  1029. send_packet_size, spi_ptr[0], spi_ptr[1], spi_ptr[2],
  1030. spi_ptr[3], spi_ptr[4], spi_ptr[5], spi_ptr[6],
  1031. spi_ptr[7]);
  1032. goto err;
  1033. }
  1034. return retval;
  1035. err:
  1036. return -EINVAL;
  1037. }
  1038. static int if_spi_send_modem_bin_xmit_img
  1039. (enum image_type type, struct image_buf *img)
  1040. {
  1041. int retval = 0;
  1042. struct sprd_image_buf sprd_img;
  1043. unsigned int data_size;
  1044. unsigned int send_size = 0;
  1045. unsigned int rest_size = 0;
  1046. unsigned int spi_size = 0;
  1047. unsigned int address = 0;
  1048. unsigned int fdl1_size = 0;
  1049. /* No Translate */
  1050. u16 crc = 0;
  1051. u16 spi_type = 0;
  1052. unsigned char *spi_ptr;
  1053. unsigned char *ptr;
  1054. int i, j = 0;
  1055. u16 sprd_packet_size = SPRD_BLOCK_SIZE;
  1056. sprd_img.tx_b = kmalloc(SPRD_BLOCK_SIZE*2, GFP_KERNEL);
  1057. if (!sprd_img.tx_b) {
  1058. pr_err("(%d) tx_b kmalloc fail.",
  1059. __LINE__);
  1060. return -ENOMEM;
  1061. }
  1062. memset(sprd_img.tx_b, 0, SPRD_BLOCK_SIZE*2);
  1063. sprd_img.rx_b = kmalloc(SPRD_BLOCK_SIZE*2, GFP_KERNEL);
  1064. if (!sprd_img.rx_b) {
  1065. pr_err("(%d) rx_b kmalloc fail.",
  1066. __LINE__);
  1067. retval = -ENOMEM;
  1068. goto err3;
  1069. }
  1070. memset(sprd_img.rx_b, 0, SPRD_BLOCK_SIZE*2);
  1071. sprd_img.encoded_tx_b = kmalloc(SPRD_BLOCK_SIZE*2, GFP_KERNEL);
  1072. if (!sprd_img.encoded_tx_b) {
  1073. pr_err("(%d) encoded_tx_b kmalloc fail.",
  1074. __LINE__);
  1075. retval = -ENOMEM;
  1076. goto err2;
  1077. }
  1078. memset(sprd_img.encoded_tx_b, 0, SPRD_BLOCK_SIZE*2);
  1079. sprd_img.decoded_rx_b = kmalloc(SPRD_BLOCK_SIZE*2, GFP_KERNEL);
  1080. if (!sprd_img.decoded_rx_b) {
  1081. pr_err("(%d) encoded_rx_b kmalloc fail.",
  1082. __LINE__);
  1083. retval = -ENOMEM;
  1084. goto err1;
  1085. }
  1086. memset(sprd_img.decoded_rx_b, 0, SPRD_BLOCK_SIZE*2);
  1087. pr_debug("(%d) if_spi_send_modem_bin_xmit_img type : %d.\n",
  1088. __LINE__, type);
  1089. memcpy(&fdl1_size, (void *)(p_spild->p_virtual_buff + 4), 4);
  1090. switch (type) {
  1091. case MODEM_MAIN:
  1092. memcpy(&img->address, (void *)(p_spild->p_virtual_buff + 8), 4);
  1093. memcpy(&img->length ,
  1094. (void *)(p_spild->p_virtual_buff + 12), 4);
  1095. img->buf = (unsigned char *)
  1096. (p_spild->p_virtual_buff + 0x30 + fdl1_size);
  1097. img->offset = img->length + fdl1_size + 0x30;
  1098. pr_debug(
  1099. "(%d) if_spi_send_modem_bin_xmit_img save MAIN to img.\n",
  1100. __LINE__);
  1101. break;
  1102. case MODEM_DSP:
  1103. memcpy(&img->address,
  1104. (void *)(p_spild->p_virtual_buff + 16), 4);
  1105. memcpy(&img->length,
  1106. (void *)(p_spild->p_virtual_buff + 20), 4);
  1107. img->buf = (unsigned char *)
  1108. (p_spild->p_virtual_buff + img->offset);
  1109. img->offset += img->length;
  1110. pr_debug("(%d) if_spi_send_modem_bin_xmit_img save DSP to img.\n",
  1111. __LINE__);
  1112. break;
  1113. case MODEM_NV:
  1114. memcpy(&img->address,
  1115. (void *)(p_spild->p_virtual_buff + 24), 4);
  1116. memcpy(&img->length,
  1117. (void *)(p_spild->p_virtual_buff + 28), 4);
  1118. img->buf = (unsigned char *)
  1119. (p_spild->p_virtual_buff + img->offset);
  1120. img->offset += img->length;
  1121. pr_debug("(%d) if_spi_send_modem_bin_xmit_img save NV to img.\n",
  1122. __LINE__);
  1123. break;
  1124. case MODEM_EFS:
  1125. memcpy(&img->address,
  1126. (void *)(p_spild->p_virtual_buff + 32), 4);
  1127. memcpy(&img->length,
  1128. (void *)(p_spild->p_virtual_buff + 36), 4);
  1129. img->buf = (unsigned char *)
  1130. (p_spild->p_virtual_buff + img->offset);
  1131. img->offset += img->length;
  1132. pr_debug("(%d) if_spi_send_modem_bin_xmit_img save EFS to img.\n",
  1133. __LINE__);
  1134. break;
  1135. case MODEM_RUN:
  1136. memset(sprd_img.encoded_tx_b, 0, SPRD_BLOCK_SIZE*2);
  1137. sprd_img.encoded_tx_size = 0;
  1138. spi_type = 0x0004;
  1139. crc = 0;
  1140. spi_ptr = sprd_img.encoded_tx_b;
  1141. spi_size = sprd_img.encoded_tx_size;
  1142. retval = if_spi_send_modem_bin_execute_cmd
  1143. (spi_ptr, spi_size, spi_type, crc, &sprd_img);
  1144. if (retval < 0) {
  1145. pr_err("(%d) if_spi_send_modem_bin_execute_cmd fail : %d",
  1146. __LINE__, retval);
  1147. goto err0;
  1148. }
  1149. return retval;
  1150. default:
  1151. pr_err("(%d) if_spi_send_modem_bin_xmit_img wrong : %d.",
  1152. __LINE__, type);
  1153. goto err0;
  1154. }
  1155. pr_debug("(%d) Start send img. size : %d\n",
  1156. __LINE__, img->length);
  1157. ptr = img->buf;
  1158. data_size = sprd_packet_size;
  1159. rest_size = img->length;
  1160. address = img->address;
  1161. M_32_SWAP(img->address);
  1162. M_32_SWAP(img->length);
  1163. /* Send Transfer Start */
  1164. sprd_img.tx_size = 8;
  1165. memcpy((sprd_img.tx_b+0), &img->address, sizeof(img->address));
  1166. memcpy((sprd_img.tx_b+4), &img->length, sizeof(img->length));
  1167. spi_type = 0x0001;
  1168. crc = sprd_crc_calc_fdl
  1169. ((unsigned short *)sprd_img.tx_b, sprd_img.tx_size);
  1170. memcpy(sprd_img.encoded_tx_b, sprd_img.tx_b, sprd_img.tx_size);
  1171. sprd_img.encoded_tx_size = sprd_img.tx_size;
  1172. spi_ptr = sprd_img.encoded_tx_b;
  1173. spi_size = sprd_img.encoded_tx_size;
  1174. pr_debug("(%d) [Transfer Start, Type = %d, Packet = %d]\n",
  1175. __LINE__, type, sprd_packet_size);
  1176. retval = if_spi_send_modem_bin_execute_cmd
  1177. (spi_ptr, spi_size, spi_type, crc, &sprd_img);
  1178. if (retval < 0) {
  1179. pr_err("(%d) if_spi_send_modem_bin_execute_cmd fail : %d",
  1180. __LINE__, retval);
  1181. goto err0;
  1182. }
  1183. M_32_SWAP(img->length);
  1184. /* Send Data */
  1185. for (i = 0; send_size < img->length; i++) {
  1186. if (rest_size < sprd_packet_size)
  1187. data_size = rest_size;
  1188. sprd_img.encoded_tx_size = sprd_packet_size;
  1189. for (j = 0; j < data_size; j++)
  1190. *(sprd_img.encoded_tx_b+j) = *(ptr + j);
  1191. spi_type = 0x0002;
  1192. crc = sprd_crc_calc_fdl
  1193. ((unsigned short *)sprd_img.encoded_tx_b,
  1194. sprd_img.encoded_tx_size);
  1195. spi_ptr = sprd_img.encoded_tx_b;
  1196. spi_size = sprd_img.encoded_tx_size;
  1197. retval = if_spi_send_modem_bin_execute_cmd
  1198. (spi_ptr, spi_size, spi_type, crc, &sprd_img);
  1199. if (retval < 0) {
  1200. pr_err("(%d) if_spi_send_modem_bin_execute_cmd fail : %d, %d",
  1201. __LINE__, retval, i);
  1202. goto err0;
  1203. }
  1204. send_size += data_size;
  1205. rest_size -= data_size;
  1206. ptr += data_size;
  1207. if (!(i % 100))
  1208. pr_debug("(%d) [%d] 0x%x size done, rest size: 0x%x\n",
  1209. __LINE__, i, send_size, rest_size);
  1210. }
  1211. /* Send Transfer End */
  1212. memset(sprd_img.encoded_tx_b, 0, SPRD_BLOCK_SIZE * 2);
  1213. sprd_img.encoded_tx_size = 0;
  1214. spi_type = 0x0003;
  1215. crc = 0;
  1216. spi_ptr = sprd_img.encoded_tx_b;
  1217. spi_size = sprd_img.encoded_tx_size;
  1218. pr_debug("(%d) [Transfer END]\n", __LINE__);
  1219. retval = if_spi_send_modem_bin_execute_cmd
  1220. (spi_ptr, spi_size, spi_type, crc, &sprd_img);
  1221. if (retval < 0) {
  1222. pr_err("(%d) if_spi_send_modem_bin_execute_cmd fail : %d",
  1223. __LINE__, retval);
  1224. goto err0;
  1225. }
  1226. err0:
  1227. kfree(sprd_img.decoded_rx_b);
  1228. err1:
  1229. kfree(sprd_img.encoded_tx_b);
  1230. err2:
  1231. kfree(sprd_img.rx_b);
  1232. err3:
  1233. kfree(sprd_img.tx_b);
  1234. return retval;
  1235. }
  1236. static void if_spi_send_modem_bin(struct work_struct *send_modem_w)
  1237. {
  1238. int retval;
  1239. struct image_buf img;
  1240. unsigned long tick1, tick2 = 0;
  1241. tick1 = jiffies_to_msecs(jiffies);
  1242. retval = if_spi_send_modem_bin_xmit_img(MODEM_MAIN, &img);
  1243. if (retval < 0) {
  1244. pr_err("(%d) if_spi_send_modem_bin_xmit_img fail : %d",
  1245. __LINE__, retval);
  1246. goto err;
  1247. }
  1248. retval = if_spi_send_modem_bin_xmit_img(MODEM_DSP, &img);
  1249. if (retval < 0) {
  1250. pr_err("(%d) if_spi_send_modem_bin_xmit_img fail : %d",
  1251. __LINE__, retval);
  1252. goto err;
  1253. }
  1254. retval = if_spi_send_modem_bin_xmit_img(MODEM_NV, &img);
  1255. if (retval < 0) {
  1256. pr_err("(%d) if_spi_send_modem_bin_xmit_img fail : %d",
  1257. __LINE__, retval);
  1258. goto err;
  1259. }
  1260. retval = if_spi_send_modem_bin_xmit_img(MODEM_EFS, &img);
  1261. if (retval < 0) {
  1262. pr_err("(%d) if_spi_send_modem_bin_xmit_img fail : %d",
  1263. __LINE__, retval);
  1264. goto err;
  1265. }
  1266. retval = if_spi_send_modem_bin_xmit_img(MODEM_RUN, &img);
  1267. if (retval < 0) {
  1268. pr_err("(%d) if_spi_send_modem_bin_xmit_img fail : %d",
  1269. __LINE__, retval);
  1270. goto err;
  1271. }
  1272. p_spild->send_modem_spi = 0;
  1273. p_spild->is_cp_reset = 0;
  1274. tick2 = jiffies_to_msecs(jiffies);
  1275. pr_info("Downloading takes %lu msec\n", (tick2-tick1));
  1276. spi_init_ipc(p_spild);
  1277. sprd_boot_done = 1;
  1278. p_spild->ril_send_cnt = 0;
  1279. p_spild->spi_state = SPI_STATE_IDLE;
  1280. err:
  1281. return;
  1282. }
  1283. static inline int _request_mem(struct spi_v_buff *od,
  1284. struct platform_device *pdev)
  1285. {
  1286. if (!p_spild->p_virtual_buff) {
  1287. od->mmio = vmalloc(od->size);
  1288. if (!od->mmio) {
  1289. pr_err("(%d) Failed to vmalloc size : %lu\n",
  1290. __LINE__, od->size);
  1291. return -EBUSY;
  1292. } else {
  1293. pr_err("(%d) vmalloc Done. mmio : 0x%08x\n",
  1294. __LINE__, (u32)od->mmio);
  1295. }
  1296. }
  1297. memset((void *)od->mmio, 0, od->size);
  1298. p_spild->p_virtual_buff = od->mmio;
  1299. return 0;
  1300. }
  1301. void spi_tx_timer_callback(unsigned long param)
  1302. {
  1303. if (p_spild->spi_state == SPI_STATE_TX_WAIT) {
  1304. p_spild->spi_timer_tx_state = SPI_STATE_TIME_OVER;
  1305. pr_err("[SPI] spi_tx_timer_callback -timer expires\n");
  1306. }
  1307. }
  1308. void spi_rx_timer_callback(unsigned long param)
  1309. {
  1310. if (p_spild->spi_state == SPI_STATE_RX_WAIT) {
  1311. p_spild->spi_timer_rx_state = SPI_STATE_TIME_OVER;
  1312. pr_err("[SPI] spi_rx_timer_callback -timer expires\n");
  1313. }
  1314. }
  1315. int spi_sema_init(void)
  1316. {
  1317. pr_info("[SPI] Srdy sema init\n");
  1318. sema_init(&p_spild->srdy_sem, 0);
  1319. p_spild->send_modem_spi = 1;
  1320. return 0;
  1321. }
  1322. void spi_work(struct work_struct *work)
  1323. {
  1324. int signal_code;
  1325. struct spi_work_type *spi_wq =
  1326. container_of(work, struct spi_work_type, work);
  1327. signal_code = spi_wq->signal_code;
  1328. if (p_spild->spi_state == SPI_STATE_END
  1329. || p_spild->spi_state == SPI_STATE_START) {
  1330. kfree(spi_wq);
  1331. return;
  1332. }
  1333. switch (signal_code) {
  1334. case SPI_WORK_SEND:
  1335. if (p_spild->spi_state == SPI_STATE_IDLE)
  1336. {
  1337. /* active the spi wakelock to prevent ap
  1338. becoming deep sleep during waiting SUB_SRDY signal. Otherwise,
  1339. may cause CP side TX queue full, because the MRDY keeping on
  1340. HIGH_LEVEL*/
  1341. if (!wake_lock_active(&p_spild->spi_wake_lock)){
  1342. wake_lock(&p_spild->spi_wake_lock);
  1343. pr_debug("[SPI] [%s](%d) spi_wakelock locked .\n",
  1344. __func__, __LINE__);
  1345. }
  1346. spi_tx_work();
  1347. }
  1348. break;
  1349. case SPI_WORK_RECEIVE:
  1350. if (p_spild->spi_state == SPI_STATE_IDLE
  1351. || p_spild->spi_state == SPI_STATE_TX_TERMINATE
  1352. || p_spild->spi_state == SPI_STATE_RX_TERMINATE)
  1353. spi_rx_work();
  1354. break;
  1355. default:
  1356. pr_err("[SPI] ERROR(No signal_code in spi_work[%d])\n",
  1357. signal_code);
  1358. break;
  1359. }
  1360. kfree(spi_wq);
  1361. if (wake_lock_active(&p_spild->spi_wake_lock)) {
  1362. wake_unlock(&p_spild->spi_wake_lock);
  1363. pr_debug("[SPI] [%s](%d) spi_wakelock unlocked .\n",
  1364. __func__, __LINE__);
  1365. }
  1366. }
  1367. static void get_mif_spi_dt_data(struct device *dev)
  1368. {
  1369. /* Initialize SPI pin value */
  1370. p_spild->gpio_ipc_mrdy = of_get_named_gpio(dev->of_node, "mif_spi,gpio_ipc_mrdy", 0);
  1371. if (gpio_is_valid(p_spild->gpio_ipc_mrdy)) {
  1372. int ret;
  1373. ret = gpio_request(p_spild->gpio_ipc_mrdy, "ipc_mrdy");
  1374. if (ret)
  1375. mif_err("Unable to request ipc_mrdy [%d]\n", p_spild->gpio_ipc_mrdy);
  1376. else {
  1377. gpio_tlmm_config(GPIO_CFG(p_spild->gpio_ipc_mrdy, GPIOMUX_FUNC_GPIO,
  1378. GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA),
  1379. GPIO_CFG_ENABLE);
  1380. gpio_direction_output(p_spild->gpio_ipc_mrdy, 0);
  1381. }
  1382. }
  1383. else {
  1384. mif_err("Failed to get is valid ipc_mrdy\n");
  1385. }
  1386. p_spild->gpio_ipc_srdy = of_get_named_gpio(dev->of_node, "mif_spi,gpio_ipc_srdy", 0);
  1387. if (gpio_is_valid(p_spild->gpio_ipc_srdy)) {
  1388. int ret;
  1389. ret = gpio_request(p_spild->gpio_ipc_srdy, "ipc_srdy");
  1390. if (ret)
  1391. mif_err("Unable to request ipc_srdy [%d]\n", p_spild->gpio_ipc_srdy);
  1392. else {
  1393. gpio_tlmm_config(GPIO_CFG(p_spild->gpio_ipc_srdy, GPIOMUX_FUNC_GPIO,
  1394. GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  1395. }
  1396. }
  1397. else {
  1398. mif_err("Failed to get is valid ipc_srdy\n");
  1399. }
  1400. p_spild->gpio_ipc_sub_mrdy = of_get_named_gpio(dev->of_node, "mif_spi,gpio_ipc_sub_mrdy", 0);
  1401. if (gpio_is_valid(p_spild->gpio_ipc_sub_mrdy)) {
  1402. int ret;
  1403. ret = gpio_request(p_spild->gpio_ipc_sub_mrdy, "ipc_sub_mrdy");
  1404. if (ret)
  1405. mif_err("Unable to request ipc_sub_mrdy [%d]\n", p_spild->gpio_ipc_sub_mrdy);
  1406. else {
  1407. gpio_tlmm_config(GPIO_CFG(p_spild->gpio_ipc_sub_mrdy, GPIOMUX_FUNC_GPIO,
  1408. GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA),GPIO_CFG_ENABLE);
  1409. gpio_direction_output(p_spild->gpio_ipc_sub_mrdy, 0);
  1410. }
  1411. }
  1412. else {
  1413. mif_err("Failed to get is valid ipc_sub_mrdy\n");
  1414. }
  1415. p_spild->gpio_ipc_sub_srdy = of_get_named_gpio(dev->of_node, "mif_spi,gpio_ipc_sub_srdy", 0);
  1416. if (gpio_is_valid(p_spild->gpio_ipc_sub_srdy)) {
  1417. int ret;
  1418. ret = gpio_request(p_spild->gpio_ipc_sub_srdy, "ipc_sub_srdy");
  1419. if (ret)
  1420. mif_err("Unable to request ipc_sub_srdy [%d]\n", p_spild->gpio_ipc_sub_srdy);
  1421. else {
  1422. gpio_tlmm_config(GPIO_CFG(p_spild->gpio_ipc_sub_srdy, GPIOMUX_FUNC_GPIO,
  1423. GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),GPIO_CFG_ENABLE);
  1424. }
  1425. }
  1426. else {
  1427. mif_err("Failed to get is valid ipc_sub_srdy\n");
  1428. }
  1429. p_spild->gpio_cp_dump_int = of_get_named_gpio(dev->of_node, "mif_spi,gpio_cp_dump_int", 0);
  1430. if (gpio_is_valid(p_spild->gpio_cp_dump_int)) {
  1431. int ret;
  1432. ret = gpio_request(p_spild->gpio_cp_dump_int, "cp_dump_int");
  1433. if (ret)
  1434. mif_err("Unable to request cp_dump_int [%d]\n", p_spild->gpio_cp_dump_int);
  1435. else {
  1436. gpio_tlmm_config(GPIO_CFG(p_spild->gpio_cp_dump_int, GPIOMUX_FUNC_GPIO,
  1437. GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  1438. }
  1439. }
  1440. else {
  1441. mif_err("Failed to get is valid cp_dump_int\n");
  1442. }
  1443. p_spild->gpio_modem_bin_srdy = p_spild->gpio_ipc_srdy;
  1444. mif_info("gpio_mrdy:%d, gpio_srdy:%d, gpio_submrdy:%d, gpio_subsrdy:%d, gpio_cpdump_int:%d\n",
  1445. p_spild->gpio_ipc_mrdy, p_spild->gpio_ipc_srdy,
  1446. p_spild->gpio_ipc_sub_mrdy, p_spild->gpio_ipc_sub_srdy,
  1447. p_spild->gpio_cp_dump_int);
  1448. }
  1449. static int __devinit if_spi_platform_probe(struct platform_device *pdev)
  1450. {
  1451. int ret;
  1452. struct spi_v_buff *od;
  1453. struct spi_platform_data *pdata = NULL;
  1454. // struct task_struct *th;
  1455. struct link_device *ld = &p_spild->ld;
  1456. pr_debug("[%s]\n", __func__);
  1457. pdata = pdev->dev.platform_data;
  1458. if (!pdata) {
  1459. pr_err("No platform data\n");
  1460. ret = -EINVAL;
  1461. goto err;
  1462. }
  1463. od = kzalloc(sizeof(struct spi_v_buff), GFP_KERNEL);
  1464. if (!od) {
  1465. pr_err("(%d) failed to allocate device\n",
  1466. __LINE__);
  1467. ret = -ENOMEM;
  1468. goto err;
  1469. }
  1470. od->base = 0;
  1471. od->size = SZ_16M; /* 16M */
  1472. if (p_spild->p_virtual_buff)
  1473. od->mmio = p_spild->p_virtual_buff;
  1474. ret = _request_mem(od, pdev);
  1475. if (ret)
  1476. {
  1477. kfree(od);
  1478. goto err;
  1479. }
  1480. sema_init(&p_spild->srdy_sem, 0);
  1481. INIT_WORK(&p_spild->send_modem_w,
  1482. if_spi_send_modem_bin);
  1483. platform_set_drvdata(pdev, od);
  1484. wake_lock_init(&p_spild->spi_wake_lock,
  1485. WAKE_LOCK_SUSPEND,
  1486. "samsung-spiwakelock");
  1487. /* Register CP_DUMP_INT interrupt handler */
  1488. ret = spi_register_isr(gpio_to_irq(p_spild->gpio_cp_dump_int),
  1489. spi_cp_dump_irq_handler,
  1490. IRQF_TRIGGER_RISING,
  1491. "spi_cp_dump_rising",
  1492. ld);
  1493. if (ret)
  1494. goto err;
  1495. /* Register SPI Srdy interrupt handler */
  1496. ret = spi_register_isr(gpio_to_irq(p_spild->gpio_ipc_srdy),
  1497. spi_srdy_irq_handler,
  1498. IRQF_TRIGGER_RISING,
  1499. "spi_srdy_rising",
  1500. ld);
  1501. if (ret)
  1502. goto err;
  1503. /* Register SPI SubSrdy interrupt handler */
  1504. ret = spi_register_isr(gpio_to_irq(p_spild->gpio_ipc_sub_srdy),
  1505. spi_subsrdy_irq_handler,
  1506. IRQF_TRIGGER_RISING,
  1507. "spi_subsrdy_rising",
  1508. ld);
  1509. if (ret)
  1510. goto err;
  1511. p_spild->boot_done = 1;
  1512. pr_info("[%s] Done\n", __func__);
  1513. err:
  1514. /* _release(od); */
  1515. return 0;
  1516. }
  1517. static int __devexit if_spi_platform_remove(struct platform_device *pdev)
  1518. {
  1519. struct spi_v_buff *od = platform_get_drvdata(pdev);
  1520. platform_set_drvdata(pdev, NULL);
  1521. kfree(od);
  1522. return 0;
  1523. }
  1524. static int if_spi_probe(struct spi_device *spi)
  1525. {
  1526. int ret=0;
  1527. p_spi = spi;
  1528. mif_info("if-spi->mode : [%d]\n", p_spi->mode);
  1529. mif_info("if_spi->bits_per_word : [%d]\n", p_spi->bits_per_word);
  1530. #if 0
  1531. p_spi->mode = SPI_MODE_1;
  1532. p_spi->bits_per_word = 32;
  1533. ret = spi_setup(p_spi);
  1534. if (ret != 0) {
  1535. pr_err("[%s] spi_setup ERROR : %d\n", __func__, ret);
  1536. return ret;
  1537. }
  1538. #endif
  1539. get_mif_spi_dt_data(&spi->dev);
  1540. pr_info("[%s]\n", __func__);
  1541. return ret;
  1542. }
  1543. static int if_spi_remove(struct spi_device *spi)
  1544. {
  1545. return 0;
  1546. }
  1547. static struct platform_driver if_spi_platform_driver = {
  1548. .probe = if_spi_platform_probe,
  1549. .remove = __devexit_p(if_spi_platform_remove),
  1550. .driver = {
  1551. .name = "if_spi_platform_driver",
  1552. },
  1553. };
  1554. static const struct of_device_id if_spi_match_table[] = {
  1555. { .compatible = "if_spi_comp",
  1556. },
  1557. {}
  1558. };
  1559. static struct spi_driver if_spi_driver = {
  1560. .probe = if_spi_probe,
  1561. .remove = __devexit_p(if_spi_remove),
  1562. .driver = {
  1563. .name = "if_spi_driver",
  1564. .of_match_table = if_spi_match_table,
  1565. .owner = THIS_MODULE,
  1566. },
  1567. };
  1568. static int spi_link_init(void)
  1569. {
  1570. int ret;
  1571. ret = spi_register_driver(&if_spi_driver);
  1572. if (ret < 0) {
  1573. pr_err("spi_register_driver() fail : %d\n", ret);
  1574. goto err;
  1575. }
  1576. ret = platform_driver_register(&if_spi_platform_driver);
  1577. if (ret < 0) {
  1578. pr_err("[%s] platform_driver_register ERROR : %d\n",
  1579. __func__, ret);
  1580. goto err;
  1581. }
  1582. /* creat work queue thread */
  1583. p_spild->spi_modem_wq = create_singlethread_workqueue("spi_modem_wq");
  1584. if (!p_spild->spi_modem_wq) {
  1585. pr_err("[%s] get workqueue thread fail\n",
  1586. __func__);
  1587. ret = -ENOMEM;
  1588. goto err;
  1589. }
  1590. pr_info("[%s] Done\n", __func__);
  1591. err:
  1592. return ret;
  1593. }
  1594. /*=====================================
  1595. * Description spi restart for CP slient reset
  1596. =====================================*/
  1597. void if_spi_thread_restart(void)
  1598. {
  1599. p_spild->send_modem_spi = 1;
  1600. p_spild->is_cp_reset = 1;
  1601. sprd_boot_done = 0;
  1602. pr_info("[IF_SPI] if_spi_thread_restart\n");
  1603. return;
  1604. }
  1605. EXPORT_SYMBOL(if_spi_thread_restart);
  1606. struct link_device *spi_create_link_device(struct platform_device *pdev)
  1607. {
  1608. struct spi_link_device *spild = NULL;
  1609. struct link_device *ld;
  1610. struct modem_data *pdata;
  1611. int ret;
  1612. int i;
  1613. /* Get the platform data */
  1614. pdata = (struct modem_data *)pdev->dev.platform_data;
  1615. if (!pdata) {
  1616. pr_err("[LNK/E] <%s> pdata == NULL\n", __func__);
  1617. goto err2;
  1618. }
  1619. mif_err("[LNK] <%s> link device = %s\n", __func__, pdata->link_name);
  1620. mif_err("[LNK] <%s> modem = %s\n", __func__, pdata->name);
  1621. /* Alloc SPI link device structure */
  1622. p_spild = spild = kzalloc(sizeof(struct spi_link_device), GFP_KERNEL);
  1623. if (!spild) {
  1624. pr_err("[LNK/E] <%s> Failed to kzalloc()\n", __func__);
  1625. goto err2;
  1626. }
  1627. ld = &spild->ld;
  1628. /* Extract modem data and SPI control data from the platform data */
  1629. ld->name = "spi";
  1630. if (ld->aligned)
  1631. pr_err("[LNK] <%s> Aligned access is required!!!\n", __func__);
  1632. /* Set attributes as a link device */
  1633. ld->send = spi_send;
  1634. ld->ioctl = spi_ioctl; //SPI_SETUP
  1635. INIT_LIST_HEAD(&ld->list);
  1636. skb_queue_head_init(&ld->sk_fmt_tx_q);
  1637. skb_queue_head_init(&ld->sk_raw_tx_q);
  1638. skb_queue_head_init(&ld->sk_rfs_tx_q);
  1639. spild->skb_txq[IPC_FMT] = &ld->sk_fmt_tx_q;
  1640. spild->skb_txq[IPC_RAW] = &ld->sk_raw_tx_q;
  1641. spild->skb_txq[IPC_RFS] = &ld->sk_rfs_tx_q;
  1642. spild->spi_wq = create_singlethread_workqueue("spi_wq");
  1643. if (!spild->spi_wq) {
  1644. pr_err("[LNK/E] <%s> Fail to create workqueue for spi_wq\n",
  1645. __func__);
  1646. goto err1;
  1647. }
  1648. spild->spi_state = SPI_STATE_END;
  1649. spild->max_ipc_dev = IPC_RFS+1; /* FMT, RAW, RFS */
  1650. for (i = 0; i < spild->max_ipc_dev; i++)
  1651. skb_queue_head_init(&spild->skb_rxq[i]);
  1652. /* Prepare a clean buffer for SPI access */
  1653. spild->buff = kzalloc(SPI_MAX_PACKET_SIZE, GFP_KERNEL);
  1654. spild->sync_buff = kzalloc(SPI_MAX_PACKET_SIZE, GFP_KERNEL);
  1655. memset(spild->buff , 0, SPI_MAX_PACKET_SIZE);
  1656. memset(spild->sync_buff , 0, SPI_MAX_PACKET_SIZE);
  1657. if (!spild->buff) {
  1658. pr_err("[LNK/E] <%s> Failed to alloc spild->buff\n", __func__);
  1659. goto err;
  1660. }
  1661. /* Create SPI Timer */
  1662. init_timer(&spild->spi_tx_timer);
  1663. spild->spi_tx_timer.expires = jiffies +
  1664. ((SPI_TIMER_TX_WAIT_TIME * HZ) / 1000);
  1665. spild->spi_tx_timer.data = 0;
  1666. spild->spi_tx_timer.function = spi_tx_timer_callback;
  1667. init_timer(&spild->spi_rx_timer);
  1668. spild->spi_rx_timer.expires = jiffies +
  1669. ((SPI_TIMER_RX_WAIT_TIME * HZ) / 1000);
  1670. spild->spi_rx_timer.data = 0;
  1671. spild->spi_rx_timer.function = spi_rx_timer_callback;
  1672. /* Create SPI device */
  1673. ret = spi_link_init();
  1674. if (ret)
  1675. goto err;
  1676. return ld;
  1677. err:
  1678. kfree(spild->buff);
  1679. err1:
  1680. kfree(spild);
  1681. err2:
  1682. return NULL;
  1683. }