i2c.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * I2C Link Layer for PN544 HCI based Driver
  3. *
  4. * Copyright (C) 2012 Intel 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 that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/crc-ccitt.h>
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/gpio.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/acpi.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <linux/nfc.h>
  30. #include <linux/firmware.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/platform_data/pn544.h>
  33. #include <asm/unaligned.h>
  34. #include <net/nfc/hci.h>
  35. #include <net/nfc/llc.h>
  36. #include <net/nfc/nfc.h>
  37. #include "pn544.h"
  38. #define PN544_I2C_FRAME_HEADROOM 1
  39. #define PN544_I2C_FRAME_TAILROOM 2
  40. /* GPIO names */
  41. #define PN544_GPIO_NAME_IRQ "pn544_irq"
  42. #define PN544_GPIO_NAME_FW "pn544_fw"
  43. #define PN544_GPIO_NAME_EN "pn544_en"
  44. /* framing in HCI mode */
  45. #define PN544_HCI_I2C_LLC_LEN 1
  46. #define PN544_HCI_I2C_LLC_CRC 2
  47. #define PN544_HCI_I2C_LLC_LEN_CRC (PN544_HCI_I2C_LLC_LEN + \
  48. PN544_HCI_I2C_LLC_CRC)
  49. #define PN544_HCI_I2C_LLC_MIN_SIZE (1 + PN544_HCI_I2C_LLC_LEN_CRC)
  50. #define PN544_HCI_I2C_LLC_MAX_PAYLOAD 29
  51. #define PN544_HCI_I2C_LLC_MAX_SIZE (PN544_HCI_I2C_LLC_LEN_CRC + 1 + \
  52. PN544_HCI_I2C_LLC_MAX_PAYLOAD)
  53. static struct i2c_device_id pn544_hci_i2c_id_table[] = {
  54. {"pn544", 0},
  55. {}
  56. };
  57. MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table);
  58. static const struct acpi_device_id pn544_hci_i2c_acpi_match[] = {
  59. {"NXP5440", 0},
  60. {}
  61. };
  62. MODULE_DEVICE_TABLE(acpi, pn544_hci_i2c_acpi_match);
  63. #define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c"
  64. /*
  65. * Exposed through the 4 most significant bytes
  66. * from the HCI SW_VERSION first byte, a.k.a.
  67. * SW RomLib.
  68. */
  69. #define PN544_HW_VARIANT_C2 0xa
  70. #define PN544_HW_VARIANT_C3 0xb
  71. #define PN544_FW_CMD_RESET 0x01
  72. #define PN544_FW_CMD_WRITE 0x08
  73. #define PN544_FW_CMD_CHECK 0x06
  74. #define PN544_FW_CMD_SECURE_WRITE 0x0C
  75. #define PN544_FW_CMD_SECURE_CHUNK_WRITE 0x0D
  76. struct pn544_i2c_fw_frame_write {
  77. u8 cmd;
  78. u16 be_length;
  79. u8 be_dest_addr[3];
  80. u16 be_datalen;
  81. u8 data[];
  82. } __packed;
  83. struct pn544_i2c_fw_frame_check {
  84. u8 cmd;
  85. u16 be_length;
  86. u8 be_start_addr[3];
  87. u16 be_datalen;
  88. u16 be_crc;
  89. } __packed;
  90. struct pn544_i2c_fw_frame_response {
  91. u8 status;
  92. u16 be_length;
  93. } __packed;
  94. struct pn544_i2c_fw_blob {
  95. u32 be_size;
  96. u32 be_destaddr;
  97. u8 data[];
  98. };
  99. struct pn544_i2c_fw_secure_frame {
  100. u8 cmd;
  101. u16 be_datalen;
  102. u8 data[];
  103. } __packed;
  104. struct pn544_i2c_fw_secure_blob {
  105. u64 header;
  106. u8 data[];
  107. };
  108. #define PN544_FW_CMD_RESULT_TIMEOUT 0x01
  109. #define PN544_FW_CMD_RESULT_BAD_CRC 0x02
  110. #define PN544_FW_CMD_RESULT_ACCESS_DENIED 0x08
  111. #define PN544_FW_CMD_RESULT_PROTOCOL_ERROR 0x0B
  112. #define PN544_FW_CMD_RESULT_INVALID_PARAMETER 0x11
  113. #define PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND 0x13
  114. #define PN544_FW_CMD_RESULT_INVALID_LENGTH 0x18
  115. #define PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR 0x19
  116. #define PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR 0x1D
  117. #define PN544_FW_CMD_RESULT_MEMORY_ERROR 0x20
  118. #define PN544_FW_CMD_RESULT_CHUNK_OK 0x21
  119. #define PN544_FW_CMD_RESULT_WRITE_FAILED 0x74
  120. #define PN544_FW_CMD_RESULT_COMMAND_REJECTED 0xE0
  121. #define PN544_FW_CMD_RESULT_CHUNK_ERROR 0xE6
  122. #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
  123. #define PN544_FW_WRITE_BUFFER_MAX_LEN 0x9f7
  124. #define PN544_FW_I2C_MAX_PAYLOAD PN544_HCI_I2C_LLC_MAX_SIZE
  125. #define PN544_FW_I2C_WRITE_FRAME_HEADER_LEN 8
  126. #define PN544_FW_I2C_WRITE_DATA_MAX_LEN MIN((PN544_FW_I2C_MAX_PAYLOAD -\
  127. PN544_FW_I2C_WRITE_FRAME_HEADER_LEN),\
  128. PN544_FW_WRITE_BUFFER_MAX_LEN)
  129. #define PN544_FW_SECURE_CHUNK_WRITE_HEADER_LEN 3
  130. #define PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN (PN544_FW_I2C_MAX_PAYLOAD -\
  131. PN544_FW_SECURE_CHUNK_WRITE_HEADER_LEN)
  132. #define PN544_FW_SECURE_FRAME_HEADER_LEN 3
  133. #define PN544_FW_SECURE_BLOB_HEADER_LEN 8
  134. #define FW_WORK_STATE_IDLE 1
  135. #define FW_WORK_STATE_START 2
  136. #define FW_WORK_STATE_WAIT_WRITE_ANSWER 3
  137. #define FW_WORK_STATE_WAIT_CHECK_ANSWER 4
  138. #define FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER 5
  139. struct pn544_i2c_phy {
  140. struct i2c_client *i2c_dev;
  141. struct nfc_hci_dev *hdev;
  142. unsigned int gpio_en;
  143. unsigned int gpio_irq;
  144. unsigned int gpio_fw;
  145. unsigned int en_polarity;
  146. u8 hw_variant;
  147. struct work_struct fw_work;
  148. int fw_work_state;
  149. char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
  150. const struct firmware *fw;
  151. u32 fw_blob_dest_addr;
  152. size_t fw_blob_size;
  153. const u8 *fw_blob_data;
  154. size_t fw_written;
  155. size_t fw_size;
  156. int fw_cmd_result;
  157. int powered;
  158. int run_mode;
  159. int hard_fault; /*
  160. * < 0 if hardware error occured (e.g. i2c err)
  161. * and prevents normal operation.
  162. */
  163. };
  164. #define I2C_DUMP_SKB(info, skb) \
  165. do { \
  166. pr_debug("%s:\n", info); \
  167. print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
  168. 16, 1, (skb)->data, (skb)->len, 0); \
  169. } while (0)
  170. static void pn544_hci_i2c_platform_init(struct pn544_i2c_phy *phy)
  171. {
  172. int polarity, retry, ret;
  173. char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
  174. int count = sizeof(rset_cmd);
  175. nfc_info(&phy->i2c_dev->dev, "Detecting nfc_en polarity\n");
  176. /* Disable fw download */
  177. gpio_set_value_cansleep(phy->gpio_fw, 0);
  178. for (polarity = 0; polarity < 2; polarity++) {
  179. phy->en_polarity = polarity;
  180. retry = 3;
  181. while (retry--) {
  182. /* power off */
  183. gpio_set_value_cansleep(phy->gpio_en,
  184. !phy->en_polarity);
  185. usleep_range(10000, 15000);
  186. /* power on */
  187. gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity);
  188. usleep_range(10000, 15000);
  189. /* send reset */
  190. dev_dbg(&phy->i2c_dev->dev, "Sending reset cmd\n");
  191. ret = i2c_master_send(phy->i2c_dev, rset_cmd, count);
  192. if (ret == count) {
  193. nfc_info(&phy->i2c_dev->dev,
  194. "nfc_en polarity : active %s\n",
  195. (polarity == 0 ? "low" : "high"));
  196. goto out;
  197. }
  198. }
  199. }
  200. nfc_err(&phy->i2c_dev->dev,
  201. "Could not detect nfc_en polarity, fallback to active high\n");
  202. out:
  203. gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity);
  204. }
  205. static void pn544_hci_i2c_enable_mode(struct pn544_i2c_phy *phy, int run_mode)
  206. {
  207. gpio_set_value_cansleep(phy->gpio_fw,
  208. run_mode == PN544_FW_MODE ? 1 : 0);
  209. gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity);
  210. usleep_range(10000, 15000);
  211. phy->run_mode = run_mode;
  212. }
  213. static int pn544_hci_i2c_enable(void *phy_id)
  214. {
  215. struct pn544_i2c_phy *phy = phy_id;
  216. pr_info("%s\n", __func__);
  217. pn544_hci_i2c_enable_mode(phy, PN544_HCI_MODE);
  218. phy->powered = 1;
  219. return 0;
  220. }
  221. static void pn544_hci_i2c_disable(void *phy_id)
  222. {
  223. struct pn544_i2c_phy *phy = phy_id;
  224. gpio_set_value_cansleep(phy->gpio_fw, 0);
  225. gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity);
  226. usleep_range(10000, 15000);
  227. gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity);
  228. usleep_range(10000, 15000);
  229. gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity);
  230. usleep_range(10000, 15000);
  231. phy->powered = 0;
  232. }
  233. static void pn544_hci_i2c_add_len_crc(struct sk_buff *skb)
  234. {
  235. u16 crc;
  236. int len;
  237. len = skb->len + 2;
  238. *skb_push(skb, 1) = len;
  239. crc = crc_ccitt(0xffff, skb->data, skb->len);
  240. crc = ~crc;
  241. *skb_put(skb, 1) = crc & 0xff;
  242. *skb_put(skb, 1) = crc >> 8;
  243. }
  244. static void pn544_hci_i2c_remove_len_crc(struct sk_buff *skb)
  245. {
  246. skb_pull(skb, PN544_I2C_FRAME_HEADROOM);
  247. skb_trim(skb, PN544_I2C_FRAME_TAILROOM);
  248. }
  249. /*
  250. * Writing a frame must not return the number of written bytes.
  251. * It must return either zero for success, or <0 for error.
  252. * In addition, it must not alter the skb
  253. */
  254. static int pn544_hci_i2c_write(void *phy_id, struct sk_buff *skb)
  255. {
  256. int r;
  257. struct pn544_i2c_phy *phy = phy_id;
  258. struct i2c_client *client = phy->i2c_dev;
  259. if (phy->hard_fault != 0)
  260. return phy->hard_fault;
  261. usleep_range(3000, 6000);
  262. pn544_hci_i2c_add_len_crc(skb);
  263. I2C_DUMP_SKB("i2c frame written", skb);
  264. r = i2c_master_send(client, skb->data, skb->len);
  265. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  266. usleep_range(6000, 10000);
  267. r = i2c_master_send(client, skb->data, skb->len);
  268. }
  269. if (r >= 0) {
  270. if (r != skb->len)
  271. r = -EREMOTEIO;
  272. else
  273. r = 0;
  274. }
  275. pn544_hci_i2c_remove_len_crc(skb);
  276. return r;
  277. }
  278. static int check_crc(u8 *buf, int buflen)
  279. {
  280. int len;
  281. u16 crc;
  282. len = buf[0] + 1;
  283. crc = crc_ccitt(0xffff, buf, len - 2);
  284. crc = ~crc;
  285. if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
  286. pr_err("CRC error 0x%x != 0x%x 0x%x\n",
  287. crc, buf[len - 1], buf[len - 2]);
  288. pr_info("%s: BAD CRC\n", __func__);
  289. print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
  290. 16, 2, buf, buflen, false);
  291. return -EPERM;
  292. }
  293. return 0;
  294. }
  295. /*
  296. * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
  297. * that i2c bus will be flushed and that next read will start on a new frame.
  298. * returned skb contains only LLC header and payload.
  299. * returns:
  300. * -EREMOTEIO : i2c read error (fatal)
  301. * -EBADMSG : frame was incorrect and discarded
  302. * -ENOMEM : cannot allocate skb, frame dropped
  303. */
  304. static int pn544_hci_i2c_read(struct pn544_i2c_phy *phy, struct sk_buff **skb)
  305. {
  306. int r;
  307. u8 len;
  308. u8 tmp[PN544_HCI_I2C_LLC_MAX_SIZE - 1];
  309. struct i2c_client *client = phy->i2c_dev;
  310. r = i2c_master_recv(client, &len, 1);
  311. if (r != 1) {
  312. nfc_err(&client->dev, "cannot read len byte\n");
  313. return -EREMOTEIO;
  314. }
  315. if ((len < (PN544_HCI_I2C_LLC_MIN_SIZE - 1)) ||
  316. (len > (PN544_HCI_I2C_LLC_MAX_SIZE - 1))) {
  317. nfc_err(&client->dev, "invalid len byte\n");
  318. r = -EBADMSG;
  319. goto flush;
  320. }
  321. *skb = alloc_skb(1 + len, GFP_KERNEL);
  322. if (*skb == NULL) {
  323. r = -ENOMEM;
  324. goto flush;
  325. }
  326. *skb_put(*skb, 1) = len;
  327. r = i2c_master_recv(client, skb_put(*skb, len), len);
  328. if (r != len) {
  329. kfree_skb(*skb);
  330. return -EREMOTEIO;
  331. }
  332. I2C_DUMP_SKB("i2c frame read", *skb);
  333. r = check_crc((*skb)->data, (*skb)->len);
  334. if (r != 0) {
  335. kfree_skb(*skb);
  336. r = -EBADMSG;
  337. goto flush;
  338. }
  339. skb_pull(*skb, 1);
  340. skb_trim(*skb, (*skb)->len - 2);
  341. usleep_range(3000, 6000);
  342. return 0;
  343. flush:
  344. if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
  345. r = -EREMOTEIO;
  346. usleep_range(3000, 6000);
  347. return r;
  348. }
  349. static int pn544_hci_i2c_fw_read_status(struct pn544_i2c_phy *phy)
  350. {
  351. int r;
  352. struct pn544_i2c_fw_frame_response response;
  353. struct i2c_client *client = phy->i2c_dev;
  354. r = i2c_master_recv(client, (char *) &response, sizeof(response));
  355. if (r != sizeof(response)) {
  356. nfc_err(&client->dev, "cannot read fw status\n");
  357. return -EIO;
  358. }
  359. usleep_range(3000, 6000);
  360. switch (response.status) {
  361. case 0:
  362. return 0;
  363. case PN544_FW_CMD_RESULT_CHUNK_OK:
  364. return response.status;
  365. case PN544_FW_CMD_RESULT_TIMEOUT:
  366. return -ETIMEDOUT;
  367. case PN544_FW_CMD_RESULT_BAD_CRC:
  368. return -ENODATA;
  369. case PN544_FW_CMD_RESULT_ACCESS_DENIED:
  370. return -EACCES;
  371. case PN544_FW_CMD_RESULT_PROTOCOL_ERROR:
  372. return -EPROTO;
  373. case PN544_FW_CMD_RESULT_INVALID_PARAMETER:
  374. return -EINVAL;
  375. case PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND:
  376. return -ENOTSUPP;
  377. case PN544_FW_CMD_RESULT_INVALID_LENGTH:
  378. return -EBADMSG;
  379. case PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR:
  380. return -ENOKEY;
  381. case PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR:
  382. return -EINVAL;
  383. case PN544_FW_CMD_RESULT_MEMORY_ERROR:
  384. return -ENOMEM;
  385. case PN544_FW_CMD_RESULT_COMMAND_REJECTED:
  386. return -EACCES;
  387. case PN544_FW_CMD_RESULT_WRITE_FAILED:
  388. case PN544_FW_CMD_RESULT_CHUNK_ERROR:
  389. return -EIO;
  390. default:
  391. return -EIO;
  392. }
  393. }
  394. /*
  395. * Reads an shdlc frame from the chip. This is not as straightforward as it
  396. * seems. There are cases where we could loose the frame start synchronization.
  397. * The frame format is len-data-crc, and corruption can occur anywhere while
  398. * transiting on i2c bus, such that we could read an invalid len.
  399. * In order to recover synchronization with the next frame, we must be sure
  400. * to read the real amount of data without using the len byte. We do this by
  401. * assuming the following:
  402. * - the chip will always present only one single complete frame on the bus
  403. * before triggering the interrupt
  404. * - the chip will not present a new frame until we have completely read
  405. * the previous one (or until we have handled the interrupt).
  406. * The tricky case is when we read a corrupted len that is less than the real
  407. * len. We must detect this here in order to determine that we need to flush
  408. * the bus. This is the reason why we check the crc here.
  409. */
  410. static irqreturn_t pn544_hci_i2c_irq_thread_fn(int irq, void *phy_id)
  411. {
  412. struct pn544_i2c_phy *phy = phy_id;
  413. struct i2c_client *client;
  414. struct sk_buff *skb = NULL;
  415. int r;
  416. if (!phy || irq != phy->i2c_dev->irq) {
  417. WARN_ON_ONCE(1);
  418. return IRQ_NONE;
  419. }
  420. client = phy->i2c_dev;
  421. dev_dbg(&client->dev, "IRQ\n");
  422. if (phy->hard_fault != 0)
  423. return IRQ_HANDLED;
  424. if (phy->run_mode == PN544_FW_MODE) {
  425. phy->fw_cmd_result = pn544_hci_i2c_fw_read_status(phy);
  426. schedule_work(&phy->fw_work);
  427. } else {
  428. r = pn544_hci_i2c_read(phy, &skb);
  429. if (r == -EREMOTEIO) {
  430. phy->hard_fault = r;
  431. nfc_hci_recv_frame(phy->hdev, NULL);
  432. return IRQ_HANDLED;
  433. } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
  434. return IRQ_HANDLED;
  435. }
  436. nfc_hci_recv_frame(phy->hdev, skb);
  437. }
  438. return IRQ_HANDLED;
  439. }
  440. static struct nfc_phy_ops i2c_phy_ops = {
  441. .write = pn544_hci_i2c_write,
  442. .enable = pn544_hci_i2c_enable,
  443. .disable = pn544_hci_i2c_disable,
  444. };
  445. static int pn544_hci_i2c_fw_download(void *phy_id, const char *firmware_name,
  446. u8 hw_variant)
  447. {
  448. struct pn544_i2c_phy *phy = phy_id;
  449. pr_info("Starting Firmware Download (%s)\n", firmware_name);
  450. strcpy(phy->firmware_name, firmware_name);
  451. phy->hw_variant = hw_variant;
  452. phy->fw_work_state = FW_WORK_STATE_START;
  453. schedule_work(&phy->fw_work);
  454. return 0;
  455. }
  456. static void pn544_hci_i2c_fw_work_complete(struct pn544_i2c_phy *phy,
  457. int result)
  458. {
  459. pr_info("Firmware Download Complete, result=%d\n", result);
  460. pn544_hci_i2c_disable(phy);
  461. phy->fw_work_state = FW_WORK_STATE_IDLE;
  462. if (phy->fw) {
  463. release_firmware(phy->fw);
  464. phy->fw = NULL;
  465. }
  466. nfc_fw_download_done(phy->hdev->ndev, phy->firmware_name, (u32) -result);
  467. }
  468. static int pn544_hci_i2c_fw_write_cmd(struct i2c_client *client, u32 dest_addr,
  469. const u8 *data, u16 datalen)
  470. {
  471. u8 frame[PN544_FW_I2C_MAX_PAYLOAD];
  472. struct pn544_i2c_fw_frame_write *framep;
  473. u16 params_len;
  474. int framelen;
  475. int r;
  476. if (datalen > PN544_FW_I2C_WRITE_DATA_MAX_LEN)
  477. datalen = PN544_FW_I2C_WRITE_DATA_MAX_LEN;
  478. framep = (struct pn544_i2c_fw_frame_write *) frame;
  479. params_len = sizeof(framep->be_dest_addr) +
  480. sizeof(framep->be_datalen) + datalen;
  481. framelen = params_len + sizeof(framep->cmd) +
  482. sizeof(framep->be_length);
  483. framep->cmd = PN544_FW_CMD_WRITE;
  484. put_unaligned_be16(params_len, &framep->be_length);
  485. framep->be_dest_addr[0] = (dest_addr & 0xff0000) >> 16;
  486. framep->be_dest_addr[1] = (dest_addr & 0xff00) >> 8;
  487. framep->be_dest_addr[2] = dest_addr & 0xff;
  488. put_unaligned_be16(datalen, &framep->be_datalen);
  489. memcpy(framep->data, data, datalen);
  490. r = i2c_master_send(client, frame, framelen);
  491. if (r == framelen)
  492. return datalen;
  493. else if (r < 0)
  494. return r;
  495. else
  496. return -EIO;
  497. }
  498. static int pn544_hci_i2c_fw_check_cmd(struct i2c_client *client, u32 start_addr,
  499. const u8 *data, u16 datalen)
  500. {
  501. struct pn544_i2c_fw_frame_check frame;
  502. int r;
  503. u16 crc;
  504. /* calculate local crc for the data we want to check */
  505. crc = crc_ccitt(0xffff, data, datalen);
  506. frame.cmd = PN544_FW_CMD_CHECK;
  507. put_unaligned_be16(sizeof(frame.be_start_addr) +
  508. sizeof(frame.be_datalen) + sizeof(frame.be_crc),
  509. &frame.be_length);
  510. /* tell the chip the memory region to which our crc applies */
  511. frame.be_start_addr[0] = (start_addr & 0xff0000) >> 16;
  512. frame.be_start_addr[1] = (start_addr & 0xff00) >> 8;
  513. frame.be_start_addr[2] = start_addr & 0xff;
  514. put_unaligned_be16(datalen, &frame.be_datalen);
  515. /*
  516. * and give our local crc. Chip will calculate its own crc for the
  517. * region and compare with ours.
  518. */
  519. put_unaligned_be16(crc, &frame.be_crc);
  520. r = i2c_master_send(client, (const char *) &frame, sizeof(frame));
  521. if (r == sizeof(frame))
  522. return 0;
  523. else if (r < 0)
  524. return r;
  525. else
  526. return -EIO;
  527. }
  528. static int pn544_hci_i2c_fw_write_chunk(struct pn544_i2c_phy *phy)
  529. {
  530. int r;
  531. r = pn544_hci_i2c_fw_write_cmd(phy->i2c_dev,
  532. phy->fw_blob_dest_addr + phy->fw_written,
  533. phy->fw_blob_data + phy->fw_written,
  534. phy->fw_blob_size - phy->fw_written);
  535. if (r < 0)
  536. return r;
  537. phy->fw_written += r;
  538. phy->fw_work_state = FW_WORK_STATE_WAIT_WRITE_ANSWER;
  539. return 0;
  540. }
  541. static int pn544_hci_i2c_fw_secure_write_frame_cmd(struct pn544_i2c_phy *phy,
  542. const u8 *data, u16 datalen)
  543. {
  544. u8 buf[PN544_FW_I2C_MAX_PAYLOAD];
  545. struct pn544_i2c_fw_secure_frame *chunk;
  546. int chunklen;
  547. int r;
  548. if (datalen > PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN)
  549. datalen = PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN;
  550. chunk = (struct pn544_i2c_fw_secure_frame *) buf;
  551. chunk->cmd = PN544_FW_CMD_SECURE_CHUNK_WRITE;
  552. put_unaligned_be16(datalen, &chunk->be_datalen);
  553. memcpy(chunk->data, data, datalen);
  554. chunklen = sizeof(chunk->cmd) + sizeof(chunk->be_datalen) + datalen;
  555. r = i2c_master_send(phy->i2c_dev, buf, chunklen);
  556. if (r == chunklen)
  557. return datalen;
  558. else if (r < 0)
  559. return r;
  560. else
  561. return -EIO;
  562. }
  563. static int pn544_hci_i2c_fw_secure_write_frame(struct pn544_i2c_phy *phy)
  564. {
  565. struct pn544_i2c_fw_secure_frame *framep;
  566. int r;
  567. framep = (struct pn544_i2c_fw_secure_frame *) phy->fw_blob_data;
  568. if (phy->fw_written == 0)
  569. phy->fw_blob_size = get_unaligned_be16(&framep->be_datalen)
  570. + PN544_FW_SECURE_FRAME_HEADER_LEN;
  571. /* Only secure write command can be chunked*/
  572. if (phy->fw_blob_size > PN544_FW_I2C_MAX_PAYLOAD &&
  573. framep->cmd != PN544_FW_CMD_SECURE_WRITE)
  574. return -EINVAL;
  575. /* The firmware also have other commands, we just send them directly */
  576. if (phy->fw_blob_size < PN544_FW_I2C_MAX_PAYLOAD) {
  577. r = i2c_master_send(phy->i2c_dev,
  578. (const char *) phy->fw_blob_data, phy->fw_blob_size);
  579. if (r == phy->fw_blob_size)
  580. goto exit;
  581. else if (r < 0)
  582. return r;
  583. else
  584. return -EIO;
  585. }
  586. r = pn544_hci_i2c_fw_secure_write_frame_cmd(phy,
  587. phy->fw_blob_data + phy->fw_written,
  588. phy->fw_blob_size - phy->fw_written);
  589. if (r < 0)
  590. return r;
  591. exit:
  592. phy->fw_written += r;
  593. phy->fw_work_state = FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER;
  594. /* SW reset command will not trig any response from PN544 */
  595. if (framep->cmd == PN544_FW_CMD_RESET) {
  596. pn544_hci_i2c_enable_mode(phy, PN544_FW_MODE);
  597. phy->fw_cmd_result = 0;
  598. schedule_work(&phy->fw_work);
  599. }
  600. return 0;
  601. }
  602. static void pn544_hci_i2c_fw_work(struct work_struct *work)
  603. {
  604. struct pn544_i2c_phy *phy = container_of(work, struct pn544_i2c_phy,
  605. fw_work);
  606. int r;
  607. struct pn544_i2c_fw_blob *blob;
  608. struct pn544_i2c_fw_secure_blob *secure_blob;
  609. switch (phy->fw_work_state) {
  610. case FW_WORK_STATE_START:
  611. pn544_hci_i2c_enable_mode(phy, PN544_FW_MODE);
  612. r = request_firmware(&phy->fw, phy->firmware_name,
  613. &phy->i2c_dev->dev);
  614. if (r < 0)
  615. goto exit_state_start;
  616. phy->fw_written = 0;
  617. switch (phy->hw_variant) {
  618. case PN544_HW_VARIANT_C2:
  619. blob = (struct pn544_i2c_fw_blob *) phy->fw->data;
  620. phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
  621. phy->fw_blob_dest_addr = get_unaligned_be32(
  622. &blob->be_destaddr);
  623. phy->fw_blob_data = blob->data;
  624. r = pn544_hci_i2c_fw_write_chunk(phy);
  625. break;
  626. case PN544_HW_VARIANT_C3:
  627. secure_blob = (struct pn544_i2c_fw_secure_blob *)
  628. phy->fw->data;
  629. phy->fw_blob_data = secure_blob->data;
  630. phy->fw_size = phy->fw->size;
  631. r = pn544_hci_i2c_fw_secure_write_frame(phy);
  632. break;
  633. default:
  634. r = -ENOTSUPP;
  635. break;
  636. }
  637. exit_state_start:
  638. if (r < 0)
  639. pn544_hci_i2c_fw_work_complete(phy, r);
  640. break;
  641. case FW_WORK_STATE_WAIT_WRITE_ANSWER:
  642. r = phy->fw_cmd_result;
  643. if (r < 0)
  644. goto exit_state_wait_write_answer;
  645. if (phy->fw_written == phy->fw_blob_size) {
  646. r = pn544_hci_i2c_fw_check_cmd(phy->i2c_dev,
  647. phy->fw_blob_dest_addr,
  648. phy->fw_blob_data,
  649. phy->fw_blob_size);
  650. if (r < 0)
  651. goto exit_state_wait_write_answer;
  652. phy->fw_work_state = FW_WORK_STATE_WAIT_CHECK_ANSWER;
  653. break;
  654. }
  655. r = pn544_hci_i2c_fw_write_chunk(phy);
  656. exit_state_wait_write_answer:
  657. if (r < 0)
  658. pn544_hci_i2c_fw_work_complete(phy, r);
  659. break;
  660. case FW_WORK_STATE_WAIT_CHECK_ANSWER:
  661. r = phy->fw_cmd_result;
  662. if (r < 0)
  663. goto exit_state_wait_check_answer;
  664. blob = (struct pn544_i2c_fw_blob *) (phy->fw_blob_data +
  665. phy->fw_blob_size);
  666. phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
  667. if (phy->fw_blob_size != 0) {
  668. phy->fw_blob_dest_addr =
  669. get_unaligned_be32(&blob->be_destaddr);
  670. phy->fw_blob_data = blob->data;
  671. phy->fw_written = 0;
  672. r = pn544_hci_i2c_fw_write_chunk(phy);
  673. }
  674. exit_state_wait_check_answer:
  675. if (r < 0 || phy->fw_blob_size == 0)
  676. pn544_hci_i2c_fw_work_complete(phy, r);
  677. break;
  678. case FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER:
  679. r = phy->fw_cmd_result;
  680. if (r < 0)
  681. goto exit_state_wait_secure_write_answer;
  682. if (r == PN544_FW_CMD_RESULT_CHUNK_OK) {
  683. r = pn544_hci_i2c_fw_secure_write_frame(phy);
  684. goto exit_state_wait_secure_write_answer;
  685. }
  686. if (phy->fw_written == phy->fw_blob_size) {
  687. secure_blob = (struct pn544_i2c_fw_secure_blob *)
  688. (phy->fw_blob_data + phy->fw_blob_size);
  689. phy->fw_size -= phy->fw_blob_size +
  690. PN544_FW_SECURE_BLOB_HEADER_LEN;
  691. if (phy->fw_size >= PN544_FW_SECURE_BLOB_HEADER_LEN
  692. + PN544_FW_SECURE_FRAME_HEADER_LEN) {
  693. phy->fw_blob_data = secure_blob->data;
  694. phy->fw_written = 0;
  695. r = pn544_hci_i2c_fw_secure_write_frame(phy);
  696. }
  697. }
  698. exit_state_wait_secure_write_answer:
  699. if (r < 0 || phy->fw_size == 0)
  700. pn544_hci_i2c_fw_work_complete(phy, r);
  701. break;
  702. default:
  703. break;
  704. }
  705. }
  706. static int pn544_hci_i2c_acpi_request_resources(struct i2c_client *client)
  707. {
  708. struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
  709. const struct acpi_device_id *id;
  710. struct gpio_desc *gpiod_en, *gpiod_irq, *gpiod_fw;
  711. struct device *dev;
  712. int ret;
  713. if (!client)
  714. return -EINVAL;
  715. dev = &client->dev;
  716. /* Match the struct device against a given list of ACPI IDs */
  717. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  718. if (!id)
  719. return -ENODEV;
  720. /* Get EN GPIO from ACPI */
  721. gpiod_en = devm_gpiod_get_index(dev, PN544_GPIO_NAME_EN, 1,
  722. GPIOD_OUT_LOW);
  723. if (IS_ERR(gpiod_en)) {
  724. nfc_err(dev, "Unable to get EN GPIO\n");
  725. return -ENODEV;
  726. }
  727. phy->gpio_en = desc_to_gpio(gpiod_en);
  728. /* Get FW GPIO from ACPI */
  729. gpiod_fw = devm_gpiod_get_index(dev, PN544_GPIO_NAME_FW, 2,
  730. GPIOD_OUT_LOW);
  731. if (IS_ERR(gpiod_fw)) {
  732. nfc_err(dev, "Unable to get FW GPIO\n");
  733. return -ENODEV;
  734. }
  735. phy->gpio_fw = desc_to_gpio(gpiod_fw);
  736. /* Get IRQ GPIO */
  737. gpiod_irq = devm_gpiod_get_index(dev, PN544_GPIO_NAME_IRQ, 0,
  738. GPIOD_IN);
  739. if (IS_ERR(gpiod_irq)) {
  740. nfc_err(dev, "Unable to get IRQ GPIO\n");
  741. return -ENODEV;
  742. }
  743. phy->gpio_irq = desc_to_gpio(gpiod_irq);
  744. /* Map the pin to an IRQ */
  745. ret = gpiod_to_irq(gpiod_irq);
  746. if (ret < 0) {
  747. nfc_err(dev, "Fail pin IRQ mapping\n");
  748. return ret;
  749. }
  750. nfc_info(dev, "GPIO resource, no:%d irq:%d\n",
  751. desc_to_gpio(gpiod_irq), ret);
  752. client->irq = ret;
  753. return 0;
  754. }
  755. #ifdef CONFIG_OF
  756. static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
  757. {
  758. struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
  759. struct device_node *pp;
  760. int ret;
  761. pp = client->dev.of_node;
  762. if (!pp) {
  763. ret = -ENODEV;
  764. goto err_dt;
  765. }
  766. /* Obtention of EN GPIO from device tree */
  767. ret = of_get_named_gpio(pp, "enable-gpios", 0);
  768. if (ret < 0) {
  769. if (ret != -EPROBE_DEFER)
  770. nfc_err(&client->dev,
  771. "Failed to get EN gpio, error: %d\n", ret);
  772. goto err_dt;
  773. }
  774. phy->gpio_en = ret;
  775. /* Configuration of EN GPIO */
  776. ret = gpio_request(phy->gpio_en, PN544_GPIO_NAME_EN);
  777. if (ret) {
  778. nfc_err(&client->dev, "Fail EN pin\n");
  779. goto err_dt;
  780. }
  781. ret = gpio_direction_output(phy->gpio_en, 0);
  782. if (ret) {
  783. nfc_err(&client->dev, "Fail EN pin direction\n");
  784. goto err_gpio_en;
  785. }
  786. /* Obtention of FW GPIO from device tree */
  787. ret = of_get_named_gpio(pp, "firmware-gpios", 0);
  788. if (ret < 0) {
  789. if (ret != -EPROBE_DEFER)
  790. nfc_err(&client->dev,
  791. "Failed to get FW gpio, error: %d\n", ret);
  792. goto err_gpio_en;
  793. }
  794. phy->gpio_fw = ret;
  795. /* Configuration of FW GPIO */
  796. ret = gpio_request(phy->gpio_fw, PN544_GPIO_NAME_FW);
  797. if (ret) {
  798. nfc_err(&client->dev, "Fail FW pin\n");
  799. goto err_gpio_en;
  800. }
  801. ret = gpio_direction_output(phy->gpio_fw, 0);
  802. if (ret) {
  803. nfc_err(&client->dev, "Fail FW pin direction\n");
  804. goto err_gpio_fw;
  805. }
  806. /* IRQ */
  807. ret = irq_of_parse_and_map(pp, 0);
  808. if (ret < 0) {
  809. nfc_err(&client->dev,
  810. "Unable to get irq, error: %d\n", ret);
  811. goto err_gpio_fw;
  812. }
  813. client->irq = ret;
  814. return 0;
  815. err_gpio_fw:
  816. gpio_free(phy->gpio_fw);
  817. err_gpio_en:
  818. gpio_free(phy->gpio_en);
  819. err_dt:
  820. return ret;
  821. }
  822. #else
  823. static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
  824. {
  825. return -ENODEV;
  826. }
  827. #endif
  828. static int pn544_hci_i2c_probe(struct i2c_client *client,
  829. const struct i2c_device_id *id)
  830. {
  831. struct pn544_i2c_phy *phy;
  832. struct pn544_nfc_platform_data *pdata;
  833. int r = 0;
  834. dev_dbg(&client->dev, "%s\n", __func__);
  835. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  836. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  837. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  838. return -ENODEV;
  839. }
  840. phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy),
  841. GFP_KERNEL);
  842. if (!phy)
  843. return -ENOMEM;
  844. INIT_WORK(&phy->fw_work, pn544_hci_i2c_fw_work);
  845. phy->fw_work_state = FW_WORK_STATE_IDLE;
  846. phy->i2c_dev = client;
  847. i2c_set_clientdata(client, phy);
  848. pdata = client->dev.platform_data;
  849. /* No platform data, using device tree. */
  850. if (!pdata && client->dev.of_node) {
  851. r = pn544_hci_i2c_of_request_resources(client);
  852. if (r) {
  853. nfc_err(&client->dev, "No DT data\n");
  854. return r;
  855. }
  856. /* Using platform data. */
  857. } else if (pdata) {
  858. if (pdata->request_resources == NULL) {
  859. nfc_err(&client->dev, "request_resources() missing\n");
  860. return -EINVAL;
  861. }
  862. r = pdata->request_resources(client);
  863. if (r) {
  864. nfc_err(&client->dev,
  865. "Cannot get platform resources\n");
  866. return r;
  867. }
  868. phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
  869. phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
  870. phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
  871. /* Using ACPI */
  872. } else if (ACPI_HANDLE(&client->dev)) {
  873. r = pn544_hci_i2c_acpi_request_resources(client);
  874. if (r) {
  875. nfc_err(&client->dev,
  876. "Cannot get ACPI data\n");
  877. return r;
  878. }
  879. } else {
  880. nfc_err(&client->dev, "No platform data\n");
  881. return -EINVAL;
  882. }
  883. pn544_hci_i2c_platform_init(phy);
  884. r = request_threaded_irq(client->irq, NULL, pn544_hci_i2c_irq_thread_fn,
  885. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  886. PN544_HCI_I2C_DRIVER_NAME, phy);
  887. if (r < 0) {
  888. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  889. goto err_rti;
  890. }
  891. r = pn544_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
  892. PN544_I2C_FRAME_HEADROOM, PN544_I2C_FRAME_TAILROOM,
  893. PN544_HCI_I2C_LLC_MAX_PAYLOAD,
  894. pn544_hci_i2c_fw_download, &phy->hdev);
  895. if (r < 0)
  896. goto err_hci;
  897. return 0;
  898. err_hci:
  899. free_irq(client->irq, phy);
  900. err_rti:
  901. if (!pdata) {
  902. gpio_free(phy->gpio_en);
  903. gpio_free(phy->gpio_fw);
  904. } else if (pdata->free_resources) {
  905. pdata->free_resources();
  906. }
  907. return r;
  908. }
  909. static int pn544_hci_i2c_remove(struct i2c_client *client)
  910. {
  911. struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
  912. struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
  913. dev_dbg(&client->dev, "%s\n", __func__);
  914. cancel_work_sync(&phy->fw_work);
  915. if (phy->fw_work_state != FW_WORK_STATE_IDLE)
  916. pn544_hci_i2c_fw_work_complete(phy, -ENODEV);
  917. pn544_hci_remove(phy->hdev);
  918. if (phy->powered)
  919. pn544_hci_i2c_disable(phy);
  920. free_irq(client->irq, phy);
  921. /* No platform data, GPIOs have been requested by this driver */
  922. if (!pdata) {
  923. gpio_free(phy->gpio_en);
  924. gpio_free(phy->gpio_fw);
  925. /* Using platform data */
  926. } else if (pdata->free_resources) {
  927. pdata->free_resources();
  928. }
  929. return 0;
  930. }
  931. static const struct of_device_id of_pn544_i2c_match[] = {
  932. { .compatible = "nxp,pn544-i2c", },
  933. {},
  934. };
  935. MODULE_DEVICE_TABLE(of, of_pn544_i2c_match);
  936. static struct i2c_driver pn544_hci_i2c_driver = {
  937. .driver = {
  938. .name = PN544_HCI_I2C_DRIVER_NAME,
  939. .owner = THIS_MODULE,
  940. .of_match_table = of_match_ptr(of_pn544_i2c_match),
  941. .acpi_match_table = ACPI_PTR(pn544_hci_i2c_acpi_match),
  942. },
  943. .probe = pn544_hci_i2c_probe,
  944. .id_table = pn544_hci_i2c_id_table,
  945. .remove = pn544_hci_i2c_remove,
  946. };
  947. module_i2c_driver(pn544_hci_i2c_driver);
  948. MODULE_LICENSE("GPL");
  949. MODULE_DESCRIPTION(DRIVER_DESC);