btqca.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Bluetooth supports for Qualcomm Atheros chips
  3. *
  4. * Copyright (c) 2015 The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * 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, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/firmware.h>
  22. #include <net/bluetooth/bluetooth.h>
  23. #include <net/bluetooth/hci_core.h>
  24. #include "btqca.h"
  25. #define VERSION "0.1"
  26. int qca_read_soc_version(struct hci_dev *hdev, u32 *soc_version)
  27. {
  28. struct sk_buff *skb;
  29. struct edl_event_hdr *edl;
  30. struct rome_version *ver;
  31. char cmd;
  32. int err = 0;
  33. bt_dev_dbg(hdev, "QCA Version Request");
  34. cmd = EDL_PATCH_VER_REQ_CMD;
  35. skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, EDL_PATCH_CMD_LEN,
  36. &cmd, HCI_EV_VENDOR, HCI_INIT_TIMEOUT);
  37. if (IS_ERR(skb)) {
  38. err = PTR_ERR(skb);
  39. bt_dev_err(hdev, "Reading QCA version information failed (%d)",
  40. err);
  41. return err;
  42. }
  43. if (skb->len != sizeof(*edl) + sizeof(*ver)) {
  44. bt_dev_err(hdev, "QCA Version size mismatch len %d", skb->len);
  45. err = -EILSEQ;
  46. goto out;
  47. }
  48. edl = (struct edl_event_hdr *)(skb->data);
  49. if (!edl) {
  50. bt_dev_err(hdev, "QCA TLV with no header");
  51. err = -EILSEQ;
  52. goto out;
  53. }
  54. if (edl->cresp != EDL_CMD_REQ_RES_EVT ||
  55. edl->rtype != EDL_APP_VER_RES_EVT) {
  56. bt_dev_err(hdev, "QCA Wrong packet received %d %d", edl->cresp,
  57. edl->rtype);
  58. err = -EIO;
  59. goto out;
  60. }
  61. ver = (struct rome_version *)(edl->data);
  62. BT_DBG("%s: Product:0x%08x", hdev->name, le32_to_cpu(ver->product_id));
  63. BT_DBG("%s: Patch :0x%08x", hdev->name, le16_to_cpu(ver->patch_ver));
  64. BT_DBG("%s: ROM :0x%08x", hdev->name, le16_to_cpu(ver->rome_ver));
  65. BT_DBG("%s: SOC :0x%08x", hdev->name, le32_to_cpu(ver->soc_id));
  66. /* QCA chipset version can be decided by patch and SoC
  67. * version, combination with upper 2 bytes from SoC
  68. * and lower 2 bytes from patch will be used.
  69. */
  70. *soc_version = (le32_to_cpu(ver->soc_id) << 16) |
  71. (le16_to_cpu(ver->rome_ver) & 0x0000ffff);
  72. if (*soc_version == 0)
  73. err = -EILSEQ;
  74. out:
  75. kfree_skb(skb);
  76. if (err)
  77. bt_dev_err(hdev, "QCA Failed to get version (%d)", err);
  78. return err;
  79. }
  80. EXPORT_SYMBOL_GPL(qca_read_soc_version);
  81. static int qca_send_reset(struct hci_dev *hdev)
  82. {
  83. struct sk_buff *skb;
  84. int err;
  85. bt_dev_dbg(hdev, "QCA HCI_RESET");
  86. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  87. if (IS_ERR(skb)) {
  88. err = PTR_ERR(skb);
  89. bt_dev_err(hdev, "QCA Reset failed (%d)", err);
  90. return err;
  91. }
  92. kfree_skb(skb);
  93. return 0;
  94. }
  95. static void qca_tlv_check_data(struct rome_config *config,
  96. const struct firmware *fw)
  97. {
  98. const u8 *data;
  99. u32 type_len;
  100. u16 tag_id, tag_len;
  101. int idx, length;
  102. struct tlv_type_hdr *tlv;
  103. struct tlv_type_patch *tlv_patch;
  104. struct tlv_type_nvm *tlv_nvm;
  105. tlv = (struct tlv_type_hdr *)fw->data;
  106. type_len = le32_to_cpu(tlv->type_len);
  107. length = (type_len >> 8) & 0x00ffffff;
  108. BT_DBG("TLV Type\t\t : 0x%x", type_len & 0x000000ff);
  109. BT_DBG("Length\t\t : %d bytes", length);
  110. config->dnld_mode = ROME_SKIP_EVT_NONE;
  111. switch (config->type) {
  112. case TLV_TYPE_PATCH:
  113. tlv_patch = (struct tlv_type_patch *)tlv->data;
  114. /* For Rome version 1.1 to 3.1, all segment commands
  115. * are acked by a vendor specific event (VSE).
  116. * For Rome >= 3.2, the download mode field indicates
  117. * if VSE is skipped by the controller.
  118. * In case VSE is skipped, only the last segment is acked.
  119. */
  120. config->dnld_mode = tlv_patch->download_mode;
  121. BT_DBG("Total Length : %d bytes",
  122. le32_to_cpu(tlv_patch->total_size));
  123. BT_DBG("Patch Data Length : %d bytes",
  124. le32_to_cpu(tlv_patch->data_length));
  125. BT_DBG("Signing Format Version : 0x%x",
  126. tlv_patch->format_version);
  127. BT_DBG("Signature Algorithm : 0x%x",
  128. tlv_patch->signature);
  129. BT_DBG("Download mode : 0x%x",
  130. tlv_patch->download_mode);
  131. BT_DBG("Reserved : 0x%x",
  132. tlv_patch->reserved1);
  133. BT_DBG("Product ID : 0x%04x",
  134. le16_to_cpu(tlv_patch->product_id));
  135. BT_DBG("Rom Build Version : 0x%04x",
  136. le16_to_cpu(tlv_patch->rom_build));
  137. BT_DBG("Patch Version : 0x%04x",
  138. le16_to_cpu(tlv_patch->patch_version));
  139. BT_DBG("Reserved : 0x%x",
  140. le16_to_cpu(tlv_patch->reserved2));
  141. BT_DBG("Patch Entry Address : 0x%x",
  142. le32_to_cpu(tlv_patch->entry));
  143. break;
  144. case TLV_TYPE_NVM:
  145. idx = 0;
  146. data = tlv->data;
  147. while (idx < length) {
  148. tlv_nvm = (struct tlv_type_nvm *)(data + idx);
  149. tag_id = le16_to_cpu(tlv_nvm->tag_id);
  150. tag_len = le16_to_cpu(tlv_nvm->tag_len);
  151. /* Update NVM tags as needed */
  152. switch (tag_id) {
  153. case EDL_TAG_ID_HCI:
  154. /* HCI transport layer parameters
  155. * enabling software inband sleep
  156. * onto controller side.
  157. */
  158. tlv_nvm->data[0] |= 0x80;
  159. /* UART Baud Rate */
  160. tlv_nvm->data[2] = config->user_baud_rate;
  161. break;
  162. case EDL_TAG_ID_DEEP_SLEEP:
  163. /* Sleep enable mask
  164. * enabling deep sleep feature on controller.
  165. */
  166. tlv_nvm->data[0] |= 0x01;
  167. break;
  168. }
  169. idx += (sizeof(u16) + sizeof(u16) + 8 + tag_len);
  170. }
  171. break;
  172. default:
  173. BT_ERR("Unknown TLV type %d", config->type);
  174. break;
  175. }
  176. }
  177. static int qca_tlv_send_segment(struct hci_dev *hdev, int seg_size,
  178. const u8 *data, enum rome_tlv_dnld_mode mode)
  179. {
  180. struct sk_buff *skb;
  181. struct edl_event_hdr *edl;
  182. struct tlv_seg_resp *tlv_resp;
  183. u8 cmd[MAX_SIZE_PER_TLV_SEGMENT + 2];
  184. int err = 0;
  185. cmd[0] = EDL_PATCH_TLV_REQ_CMD;
  186. cmd[1] = seg_size;
  187. memcpy(cmd + 2, data, seg_size);
  188. if (mode == ROME_SKIP_EVT_VSE_CC || mode == ROME_SKIP_EVT_VSE)
  189. return __hci_cmd_send(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2,
  190. cmd);
  191. skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2, cmd,
  192. HCI_EV_VENDOR, HCI_INIT_TIMEOUT);
  193. if (IS_ERR(skb)) {
  194. err = PTR_ERR(skb);
  195. bt_dev_err(hdev, "QCA Failed to send TLV segment (%d)", err);
  196. return err;
  197. }
  198. if (skb->len != sizeof(*edl) + sizeof(*tlv_resp)) {
  199. bt_dev_err(hdev, "QCA TLV response size mismatch");
  200. err = -EILSEQ;
  201. goto out;
  202. }
  203. edl = (struct edl_event_hdr *)(skb->data);
  204. if (!edl) {
  205. bt_dev_err(hdev, "TLV with no header");
  206. err = -EILSEQ;
  207. goto out;
  208. }
  209. tlv_resp = (struct tlv_seg_resp *)(edl->data);
  210. if (edl->cresp != EDL_CMD_REQ_RES_EVT ||
  211. edl->rtype != EDL_TVL_DNLD_RES_EVT || tlv_resp->result != 0x00) {
  212. bt_dev_err(hdev, "QCA TLV with error stat 0x%x rtype 0x%x (0x%x)",
  213. edl->cresp, edl->rtype, tlv_resp->result);
  214. err = -EIO;
  215. }
  216. out:
  217. kfree_skb(skb);
  218. return err;
  219. }
  220. static int qca_download_firmware(struct hci_dev *hdev,
  221. struct rome_config *config)
  222. {
  223. const struct firmware *fw;
  224. const u8 *segment;
  225. int ret, remain, i = 0;
  226. bt_dev_info(hdev, "QCA Downloading %s", config->fwname);
  227. ret = request_firmware(&fw, config->fwname, &hdev->dev);
  228. if (ret) {
  229. bt_dev_err(hdev, "QCA Failed to request file: %s (%d)",
  230. config->fwname, ret);
  231. return ret;
  232. }
  233. qca_tlv_check_data(config, fw);
  234. segment = fw->data;
  235. remain = fw->size;
  236. while (remain > 0) {
  237. int segsize = min(MAX_SIZE_PER_TLV_SEGMENT, remain);
  238. bt_dev_dbg(hdev, "Send segment %d, size %d", i++, segsize);
  239. remain -= segsize;
  240. /* The last segment is always acked regardless download mode */
  241. if (!remain || segsize < MAX_SIZE_PER_TLV_SEGMENT)
  242. config->dnld_mode = ROME_SKIP_EVT_NONE;
  243. ret = qca_tlv_send_segment(hdev, segsize, segment,
  244. config->dnld_mode);
  245. if (ret)
  246. break;
  247. segment += segsize;
  248. }
  249. release_firmware(fw);
  250. return ret;
  251. }
  252. int qca_set_bdaddr_rome(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  253. {
  254. struct sk_buff *skb;
  255. u8 cmd[9];
  256. int err;
  257. cmd[0] = EDL_NVM_ACCESS_SET_REQ_CMD;
  258. cmd[1] = 0x02; /* TAG ID */
  259. cmd[2] = sizeof(bdaddr_t); /* size */
  260. memcpy(cmd + 3, bdaddr, sizeof(bdaddr_t));
  261. skb = __hci_cmd_sync_ev(hdev, EDL_NVM_ACCESS_OPCODE, sizeof(cmd), cmd,
  262. HCI_EV_VENDOR, HCI_INIT_TIMEOUT);
  263. if (IS_ERR(skb)) {
  264. err = PTR_ERR(skb);
  265. bt_dev_err(hdev, "QCA Change address command failed (%d)", err);
  266. return err;
  267. }
  268. kfree_skb(skb);
  269. return 0;
  270. }
  271. EXPORT_SYMBOL_GPL(qca_set_bdaddr_rome);
  272. int qca_uart_setup(struct hci_dev *hdev, uint8_t baudrate,
  273. enum qca_btsoc_type soc_type, u32 soc_ver)
  274. {
  275. struct rome_config config;
  276. int err;
  277. u8 rom_ver;
  278. bt_dev_dbg(hdev, "QCA setup on UART");
  279. config.user_baud_rate = baudrate;
  280. /* Download rampatch file */
  281. config.type = TLV_TYPE_PATCH;
  282. if (soc_type == QCA_WCN3990) {
  283. /* Firmware files to download are based on ROM version.
  284. * ROM version is derived from last two bytes of soc_ver.
  285. */
  286. rom_ver = ((soc_ver & 0x00000f00) >> 0x04) |
  287. (soc_ver & 0x0000000f);
  288. snprintf(config.fwname, sizeof(config.fwname),
  289. "qca/crbtfw%02x.tlv", rom_ver);
  290. } else {
  291. snprintf(config.fwname, sizeof(config.fwname),
  292. "qca/rampatch_%08x.bin", soc_ver);
  293. }
  294. err = qca_download_firmware(hdev, &config);
  295. if (err < 0) {
  296. bt_dev_err(hdev, "QCA Failed to download patch (%d)", err);
  297. return err;
  298. }
  299. /* Download NVM configuration */
  300. config.type = TLV_TYPE_NVM;
  301. if (soc_type == QCA_WCN3990)
  302. snprintf(config.fwname, sizeof(config.fwname),
  303. "qca/crnv%02x.bin", rom_ver);
  304. else
  305. snprintf(config.fwname, sizeof(config.fwname),
  306. "qca/nvm_%08x.bin", soc_ver);
  307. err = qca_download_firmware(hdev, &config);
  308. if (err < 0) {
  309. bt_dev_err(hdev, "QCA Failed to download NVM (%d)", err);
  310. return err;
  311. }
  312. /* Perform HCI reset */
  313. err = qca_send_reset(hdev);
  314. if (err < 0) {
  315. bt_dev_err(hdev, "QCA Failed to run HCI_RESET (%d)", err);
  316. return err;
  317. }
  318. bt_dev_info(hdev, "QCA setup on UART is completed");
  319. return 0;
  320. }
  321. EXPORT_SYMBOL_GPL(qca_uart_setup);
  322. MODULE_AUTHOR("Ben Young Tae Kim <ytkim@qca.qualcomm.com>");
  323. MODULE_DESCRIPTION("Bluetooth support for Qualcomm Atheros family ver " VERSION);
  324. MODULE_VERSION(VERSION);
  325. MODULE_LICENSE("GPL");