blob: 2f8b2eddf3713259681a7c12f752bdfcf9f66355 [file] [log] [blame]
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -07001/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -07002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <arch/ops.h>
30#include <sys/types.h>
31#include <kernel/thread.h>
32#include <debug.h>
33#include <err.h>
34#include <reg.h>
35#include <string.h>
36#include <malloc.h>
37#include <stdlib.h>
38#include <ufs_hw.h>
39#include <utp.h>
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -070040#include <ufs.h>
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -070041#include <platform/iomap.h>
42#include <platform/clock.h>
43#include <arch/ops.h>
44#include <endian.h>
45#include <stdlib.h>
46#include <sys/types.h>
47
48void utp_process_req_completion(struct ufs_req_irq_type *irq)
49{
50 struct ufs_req_node *req;
51 struct list_node *prev;
52 uint32_t val;
53
54 /* Make sure we have more nodes than just the head in the list. */
55 if (list_next(irq->list, irq->list) == NULL)
56 {
Sridhar Parasuram56d09f32014-11-05 15:58:30 -080057 dprintf(CRITICAL, "%s:%d UTRD/ UTMRD processed signalled and the wait queue is empty\n", __func__, __LINE__);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -070058 ASSERT(0);
59 }
60
61 /* Read the door bell register. */
62 val = readl(irq->door_bell_reg);
63
64 list_for_every_entry(irq->list, req, struct ufs_req_node, list_node)
65 {
66 if (!(req->door_bell_bit & val))
67 {
68 /* Transaction is complete: Either transaction completed in a normal way.
69 * Delete and Signal all requests that have completed.
70 */
71 prev = req->list_node.prev;
72 /* TODO: move delete to the caller function. */
73 list_delete(&(req->list_node));
74
75 if (event_signal(req->event, false))
76 {
Sridhar Parasuram56d09f32014-11-05 15:58:30 -080077 dprintf(CRITICAL, "%s:%d Event signal failed.\n",__func__, __LINE__);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -070078 ASSERT(0);
79
80 }
81 req = containerof(prev, struct ufs_req_node, list_node);
82 }
83 }
84
85 return;
86}
87
88/* Always called within critical section: utrd_bitmap_mutex/ utmrd_bitmap_mutex. */
89static uint32_t utp_get_door_bell_bit(uint32_t reg, uint32_t *reg_bitmap, uint32_t *bit_num)
90{
91 uint32_t val = 0;
92 uint32_t doorbell_bit_val;
93 uint32_t found = 0;
94
95 *bit_num = 0;
96
97 val = readl(reg) | *reg_bitmap;
98 doorbell_bit_val = 1;
99
100 /* Find an empty slot. */
101 do
102 {
103 (*bit_num)++;
104
105 if (!(doorbell_bit_val & val))
106 {
107 found = 1;
108 *reg_bitmap |= doorbell_bit_val;
109 break;
110 }
111 doorbell_bit_val <<= 1;
112 }while (doorbell_bit_val <= (uint32_t) (1 << 31));
113
114 if (!found)
115 {
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700116 doorbell_bit_val = 0;
Sridhar Parasuram56d09f32014-11-05 15:58:30 -0800117 dprintf(CRITICAL, "%s:%d Unable to find a free slot for transaction.\n",__func__, __LINE__);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700118 }
119
120 return doorbell_bit_val;
121}
122
123static void utp_ring_door_bell(uint32_t reg, uint32_t doorbell_bit)
124{
125 writel(doorbell_bit, reg);
126}
127
128static int utp_utrd_process_timeout_req(struct ufs_dev *dev,
129 struct utp_utrd_req_build_type *utrd_req,
130 struct ufs_req_node *req)
131{
132 switch (utrd_req->req_upiu->trans_type)
133 {
134 case UPIU_TYPE_NOP_OUT:
135 writel(~req->door_bell_bit, UFS_UTRLCLR(dev->base));
136 return -UFS_RETRY;
137 default:
138 /* TODO : Add ufs hci sw reset.*/
139 ASSERT(0);
140 return -UFS_FAILURE;
141 }
142}
143
144static int utp_remove_from_bitmap(struct utp_bitmap_access_type *req)
145{
146
147 if (mutex_acquire(req->mutx))
148 {
149 return -UFS_FAILURE;
150 }
151
152 *(req->bitmap) &= ~req->door_bell_bit;
153
154 if (mutex_release(req->mutx))
155 {
156 return -UFS_FAILURE;
157 }
158
159 return UFS_SUCCESS;
160}
161
162static void utp_enqueue_utrd_fill_desc(struct utp_trans_req_desc *desc, struct utp_utrd_req_build_type *utrd_req)
163{
164 /* Fill transfer desc. */
165 memset(desc, 0, UPIU_HDR_LEN);
166 desc->cmd_type_dd_irq = UTP_REQ_BUILD_CMD_DD_IRQ_FIELD(utrd_req->cmd_type, utrd_req->dd, utrd_req->irq);
167 desc->overall_cmd_status = utrd_req->ocs;
168 /* Bits 0 - 6 are reserved in cmd_desc_base_addr[0] field. */
169 desc->cmd_desc_base_addr[0] = ((uint32_t) utrd_req->req_upiu) & 0xC0;
170 desc->cmd_desc_base_addr[1] = ((uint32_t) utrd_req->req_upiu >> 8) & 0xFF;
171 desc->cmd_desc_base_addr[2] = ((uint32_t) utrd_req->req_upiu >> 16) & 0xFF;
172 desc->cmd_desc_base_addr[3] = ((uint32_t) utrd_req->req_upiu >> 24) & 0xFF;
173 desc->resp_upiu_offset = ROUNDUP(utrd_req->req_upiu_len, UPIU_HDR_LEN) / 4;
174 desc->resp_upiu_len = utrd_req->resp_upiu_len;
175
176 if (utrd_req->dd != UTRD_NO_DATA_TRANSFER)
177 {
178 /* Data transfer command.
179 * Fill in PRDT data.
180 */
181 desc->prdt_offset = utrd_req->prdt_offset / 4;
182 desc->prdt_len = utrd_req->prdt_len;
183 }
184
185 /* Flush UTRD to memory. */
186 cache_clean_invalidate_unaligned_start_addr((addr_t)desc, sizeof(struct utp_trans_req_desc));
187}
188
189static struct utp_trans_req_desc* utp_get_desc_slot_addr(struct ufs_dev *dev, struct utp_utrd_req_build_type *utrd_req, uint32_t *door_bell_val)
190{
191 struct utp_trans_req_desc *desc = NULL;
192 uint32_t door_bell_slot;
193
194 if (mutex_acquire(&(dev->utrd_data.bitmap_mutex)))
195 {
196 goto utp_get_desc_slot_addr_err;
197 }
198
199 *door_bell_val = utp_get_door_bell_bit(UFS_UTRLDBR(dev->base), &dev->utrd_data.bitmap, &door_bell_slot);
200 if (!(*door_bell_val))
201 {
202 goto utp_get_desc_slot_addr_err;
203 }
204
205 if (mutex_release(&(dev->utrd_data.bitmap_mutex)))
206 {
207 goto utp_get_desc_slot_addr_err;
208 }
209
210 desc = (struct utp_trans_req_desc *) ((addr_t)dev->utrd_data.list_base_addr + (door_bell_slot - 1) * sizeof(struct utp_trans_req_desc));
211
212utp_get_desc_slot_addr_err:
213 return desc;
214}
215
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -0700216int utp_poll_utrd_complete(struct ufs_dev *dev)
217{
218 int ret;
219 struct ufs_req_irq_type irq;
Sridhar Parasuramea079c02014-10-09 16:37:27 -0700220 uint32_t val, base, retry = 0;
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -0700221 base = dev->base;
222 val = readl(UFS_IS(base));
223 irq.irq_handled = 0;
224 /* Wait till the desc has been processed. */
225 while(((val & UFS_IS_UTRCS) == 0) && ((val & UFS_IS_UTMRCS) == 0))
226 {
227 val = readl(UFS_IS(base));
Sridhar Parasuramea079c02014-10-09 16:37:27 -0700228 retry++;
229 udelay(1);
230 if(retry == UTP_MAX_COMMAND_RETRY)
231 {
Sridhar Parasuram56d09f32014-11-05 15:58:30 -0800232 dprintf(CRITICAL, "%s:%d UTP command never completed.\n", __func__, __LINE__);
233 return ERR_TIMED_OUT;
Sridhar Parasuramea079c02014-10-09 16:37:27 -0700234 }
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -0700235#ifdef DEBUG_UFS
236 dprintf(INFO, "Waiting for UTRCS/URMRCS Completion...\n");
237#endif
238 }
239 if (readl(UFS_IS(base)) & UFS_IS_UTRCS)
240 {
241 val = readl(UFS_IS(base)) & UFS_IS_UTRCS;
242 writel(UFS_IS_UTRCS, UFS_IS(base));
243 irq.irq_handled = UFS_IS_UTRCS;
244 irq.list = &(dev->utrd_data.list_head.list_node);
245 irq.door_bell_reg = UFS_UTRLDBR(base);
246 utp_process_req_completion(&irq);
247 ret = INT_NO_RESCHEDULE;
248 }
249 else if (readl(UFS_IS(base)) & (UFS_IS_UTMRCS))
250 {
251 val = readl(UFS_IS(base)) & UFS_IS_UTMRCS;
252 writel(UFS_IS_UTMRCS, UFS_IS(base));
253 irq.irq_handled = UFS_IS_UTMRCS;
254 irq.list = &(dev->utmrd_data.list_head.list_node);
255 utp_process_req_completion(&irq);
256 ret = INT_NO_RESCHEDULE;
257 }
258 return ret;
259}
260
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700261static int utp_enqueue_utrd(struct ufs_dev *dev, struct utp_utrd_req_build_type *utrd_req)
262{
263 int ret;
264 struct utp_trans_req_desc *desc;
265 event_t utrd_evt;
266 struct ufs_req_node req;
267 uint32_t door_bell_bit_val;
268 struct utp_bitmap_access_type bitmap_req;
269
270 ret = UFS_SUCCESS;
271
272 event_init(&utrd_evt, false, EVENT_FLAG_AUTOUNSIGNAL);
273
274 desc = utp_get_desc_slot_addr(dev, utrd_req, &door_bell_bit_val);
275 if (!desc)
276 {
277 ret = UFS_FAILURE;
278 goto utp_enqueue_utrd_err;
279 }
280
281 /* Check register UTRLRSR and make sure it is read ‘1’ before continuing. */
282 if (!readl(UFS_UTRLRSR(dev->base)))
283 {
284 ret = -UFS_FAILURE;
285 goto utp_enqueue_utrd_err;
286 }
287
288 utp_enqueue_utrd_fill_desc(desc, utrd_req);
289
290 req.door_bell_bit = door_bell_bit_val;
291 req.event = &utrd_evt;
292
293 /* Enqueue the req in the device utrd list. */
294 list_add_head(&(dev->utrd_data.list_head.list_node), &(req.list_node));
295
296 dsb();
297
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -0700298#ifdef DEBUG_UFS
299 // print IS before write
300 ufs_dump_is_register(dev);
301#endif
302
303 utp_ring_door_bell(UFS_UTRLDBR(dev->base), door_bell_bit_val);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700304
305 dsb();
306
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -0700307#ifdef DEBUG_UFS
308 // print IS after write
309 ufs_dump_is_register(dev);
310#endif
311 ret = utp_poll_utrd_complete(dev);
312
Sridhar Parasuram56d09f32014-11-05 15:58:30 -0800313 if (ret == ERR_TIMED_OUT)
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700314 {
Sridhar Parasuram56d09f32014-11-05 15:58:30 -0800315 /* Transaction not completed even after timeout ms. */
316 dprintf(CRITICAL, "%s:%d Transaction timeout after polling %d times\n",__func__, __LINE__, UTP_MAX_COMMAND_RETRY);
317 ret = utp_utrd_process_timeout_req(dev, utrd_req, &req);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700318 goto utp_enqueue_utrd_err;
319 }
320 else
321 {
322 /* Reset ret before returning. */
323 ret = UFS_SUCCESS;
324
325 /* Force read UTRD from memory. */
326 dsb();
327 cache_clean_invalidate_unaligned_start_addr((addr_t) desc, sizeof(struct ufs_req_node));
328
329 /* Check the response. */
330 if (desc->overall_cmd_status != UTRD_OCS_SUCCESS)
331 {
Sridhar Parasuram56d09f32014-11-05 15:58:30 -0800332 dprintf(CRITICAL, "%s:%d Command failed. command type = %x\n", __func__, __LINE__, utrd_req->cmd_type);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700333 ret = -UFS_FAILURE;
334 goto utp_enqueue_utrd_err;
335 }
336 }
337
338 /* Signal slot as free. */
339 bitmap_req.bitmap = &dev->utrd_data.bitmap;
340 bitmap_req.door_bell_bit = req.door_bell_bit;
341 bitmap_req.mutx = &(dev->utrd_data.bitmap_mutex);
342
343 ret = utp_remove_from_bitmap(&bitmap_req);
344 if (ret)
345 goto utp_enqueue_utrd_err;
346
347utp_enqueue_utrd_err:
348 return ret;
349}
350
351static int utp_get_prdt_len(uint32_t data_len, uint32_t *num_prdt)
352{
353 /* Calculate the prdt entries required. */
354 *num_prdt = ROUNDUP(data_len, UTP_MAX_PRD_DATA_BYTE_CNT);
355 *num_prdt >>= UTP_MAX_PRD_DATA_BYTE_CNT_BYTE_SHIFT;
356
357 if (*num_prdt > UTP_MAX_PRD_TABLE_ENTRIES)
358 {
Sridhar Parasuram56d09f32014-11-05 15:58:30 -0800359 dprintf(CRITICAL, "%s:%d Data length exceeds for a single upiu transfer.\n", __func__,__LINE__);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700360 return -UFS_FAILURE;
361 }
362
363 return UFS_SUCCESS;
364}
365
366static int utp_fill_req_upiu(struct ufs_dev *dev, struct upiu_req_build_type *upiu_data, struct upiu_gen_hdr *req_upiu)
367{
368 memset(req_upiu, 0, UPIU_HDR_LEN);
369
370 if (upiu_data->trans_type == UPIU_TYPE_QUERY_REQ)
371 {
372 /* Fill in query specific fields. */
373 if (utp_build_query_req_upiu((struct upiu_trans_mgmt_query_hdr *) req_upiu, upiu_data))
374 {
375 return -UFS_FAILURE;
376 }
377 }
378
379 /* If a data transfer cmd, check the alignment and length of the buffer. */
380 if (upiu_data->expected_data_len)
381 {
382 if (upiu_data->data_buffer_addr & 0x3)
383 {
Sridhar Parasuram56d09f32014-11-05 15:58:30 -0800384 dprintf(CRITICAL, "%s:%d Alignment and length check failed for data tranfer command.\n", __func__, __LINE__);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700385 return -UFS_FAILURE;
386 }
387 }
388
389 req_upiu->basic_hdr.trans_type = upiu_data->trans_type;
390 req_upiu->basic_hdr.flags = upiu_data->flags;
391 req_upiu->basic_hdr.cmd_set_type = upiu_data->cmd_set_type;
392 req_upiu->basic_hdr.data_seg_len = upiu_data->data_seg_len;
393 req_upiu->basic_hdr.lun = upiu_data->lun;
394 req_upiu->basic_hdr.total_ehs_len = upiu_data->ehs_len;
395 req_upiu->basic_hdr.task_tag = atomic_add((int *) &(dev->utrd_data.task_id), 1);
396 if (upiu_data->cdb)
397 memcpy(&(req_upiu->trans_specific_fields[4]), (void *) upiu_data->cdb, 16);
398 /* If command upiu, fill in data length. */
399 if (req_upiu->basic_hdr.trans_type == UPIU_TYPE_COMMAND)
400 ((struct upiu_cmd_hdr *)req_upiu)->data_expected_len = BE32(upiu_data->expected_data_len);
401
402 return UFS_SUCCESS;
403
404}
405
406static void utp_fill_utrd_properties(struct upiu_req_build_type *upiu_data,
407 struct utp_utrd_req_build_type *utrd,
408 struct utrd_cmd_desc *cmd_desc)
409{
410 utrd->cmd_type = upiu_data->cmd_type;
411 utrd->dd = upiu_data->dd;
412 utrd->irq = UTRD_IRQ_CMD;
413 utrd->prdt_offset = UPIU_HDR_LEN + cmd_desc->resp_upiu_len;
414 utrd->prdt_len = cmd_desc->num_prdt;
415 utrd->req_upiu = (struct upiu_basic_hdr *) cmd_desc->req_upiu;
416 utrd->req_upiu_len = UPIU_HDR_LEN;
417 utrd->resp_upiu_len = cmd_desc->resp_upiu_len;
418 utrd->ocs = 0xF;
419 utrd->timeout = upiu_data->timeout_msecs;
420}
421
422static void utp_fill_prdt_entries(struct upiu_req_build_type *upiu_data, struct utp_prdt_entry *prdt_entry)
423{
424 uint64_t buf;
425 uint64_t bytes_remaining;
426 uint32_t prd_dbc;
427
428 buf = upiu_data->data_buffer_addr;
429 bytes_remaining = upiu_data->expected_data_len;
430
431 while (bytes_remaining)
432 {
433 prdt_entry->data_base_addr = buf;
434 prdt_entry->data_upper_addr = buf >> 32;
435 prd_dbc = MIN(UTP_MAX_PRD_DATA_BYTE_CNT, bytes_remaining) - 1;
436 prdt_entry->data_byte_cnt = prd_dbc;
437 if (bytes_remaining <= UTP_MAX_PRD_DATA_BYTE_CNT)
438 break;
439 buf += UTP_MAX_PRD_DATA_BYTE_CNT;
440 bytes_remaining -= UTP_MAX_PRD_DATA_BYTE_CNT;
441 prdt_entry++;
442 }
443
444}
445
446int utp_enqueue_upiu(struct ufs_dev *dev, struct upiu_req_build_type *upiu_data)
447{
448 struct upiu_gen_hdr *req_upiu;
449 struct utp_utrd_req_build_type utrd;
450 uint32_t num_prdt;
451 struct utp_prdt_entry *prdt_entry;
452 int ret = UFS_SUCCESS;
453 uint32_t resp_len;
454 uint32_t cmd_desc_len;
455 struct utrd_cmd_desc cmd_desc;
456
457 /* Round up resp_upiu_len to a DWORD boundary.
458 * Also, make sure it is of min required length.
459 */
460 resp_len = ROUNDUP(upiu_data->resp_data_len, 4) + UPIU_HDR_LEN;
461
462 if (utp_get_prdt_len(upiu_data->expected_data_len, &num_prdt))
463 return -UFS_FAILURE;
464
465 /* Calculate the length. */
466 cmd_desc_len = UPIU_HDR_LEN + resp_len + num_prdt * sizeof(struct utp_prdt_entry);
467
468 /* Allocate memory for UTP Command Descriptor. */
469 req_upiu = (struct upiu_gen_hdr*) memalign((size_t ) lcm(CACHE_LINE, UTP_CMD_DESC_BASE_ALIGNMENT_SIZE), ROUNDUP(cmd_desc_len, CACHE_LINE));
470 if (!req_upiu)
471 {
Sridhar Parasuram56d09f32014-11-05 15:58:30 -0800472 dprintf(CRITICAL, "%s:%d Unable to allocate request upiu\n",__func__, __LINE__);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700473 return -UFS_FAILURE;
474 }
475
476 /* Fill req upiu. */
477 ret = utp_fill_req_upiu(dev, upiu_data, req_upiu);
478 if (ret)
479 {
480 goto utp_enqueue_upiu_err;
481 }
482
483 /* Fill UTRD properties. */
484 cmd_desc.num_prdt = num_prdt;
485 cmd_desc.req_upiu = req_upiu;
486 cmd_desc.resp_upiu_len = resp_len;
487 utp_fill_utrd_properties(upiu_data, &utrd, &cmd_desc);
488
489 prdt_entry = (struct utp_prdt_entry *) ((uint32_t) req_upiu + UPIU_HDR_LEN + resp_len);
490
491 /* Fill PRDT entries. */
492 if (num_prdt)
493 utp_fill_prdt_entries(upiu_data, prdt_entry);
494
495 /* Flush req_upiu */
496 dsb();
497 arch_clean_invalidate_cache_range((addr_t) req_upiu, cmd_desc_len);
498
499 /* Check the response. */
500 ret = utp_enqueue_utrd(dev, &utrd);
501 if (ret)
502 {
Sridhar Parasuram56d09f32014-11-05 15:58:30 -0800503 dprintf(CRITICAL, "%s:%d Command failed. command = %x\n", __func__, __LINE__, req_upiu->basic_hdr.trans_type);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700504 goto utp_enqueue_upiu_err;
505 }
506
507 /* UPIU processed. Invalidate cache to update resp. */
508 arch_invalidate_cache_range((addr_t) req_upiu, cmd_desc_len);
509
510 /* Save the response. */
511 memcpy(upiu_data->resp_ptr, (void *) ((uint32_t)req_upiu + UPIU_HDR_LEN), upiu_data->resp_len);
512 memcpy((void *) upiu_data->resp_data_ptr, (void *) ((uint32_t)req_upiu + 2 * UPIU_HDR_LEN), upiu_data->resp_data_len);
513
514utp_enqueue_upiu_err:
515 free(req_upiu);
516 return ret;
517}