blob: 317cd69cb951524a2c7e25f8c4f1c4dd60b93191 [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 {
57 dprintf(CRITICAL, "UTRD/ UTMRD processed signalled and the wait queue is empty\n");
58 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 {
77 dprintf(CRITICAL, "Event signal failed.\n");
78 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 {
116 /* TODO: This case shouldd wait on a semaphore instead od returning error.
117 * Return error until LK adds semaphore support.
118 */
119 doorbell_bit_val = 0;
120 dprintf(CRITICAL, "Unable to find a free slot for transaction.\n");
121 }
122
123 return doorbell_bit_val;
124}
125
126static void utp_ring_door_bell(uint32_t reg, uint32_t doorbell_bit)
127{
128 writel(doorbell_bit, reg);
129}
130
131static int utp_utrd_process_timeout_req(struct ufs_dev *dev,
132 struct utp_utrd_req_build_type *utrd_req,
133 struct ufs_req_node *req)
134{
135 switch (utrd_req->req_upiu->trans_type)
136 {
137 case UPIU_TYPE_NOP_OUT:
138 writel(~req->door_bell_bit, UFS_UTRLCLR(dev->base));
139 return -UFS_RETRY;
140 default:
141 /* TODO : Add ufs hci sw reset.*/
142 ASSERT(0);
143 return -UFS_FAILURE;
144 }
145}
146
147static int utp_remove_from_bitmap(struct utp_bitmap_access_type *req)
148{
149
150 if (mutex_acquire(req->mutx))
151 {
152 return -UFS_FAILURE;
153 }
154
155 *(req->bitmap) &= ~req->door_bell_bit;
156
157 if (mutex_release(req->mutx))
158 {
159 return -UFS_FAILURE;
160 }
161
162 return UFS_SUCCESS;
163}
164
165static void utp_enqueue_utrd_fill_desc(struct utp_trans_req_desc *desc, struct utp_utrd_req_build_type *utrd_req)
166{
167 /* Fill transfer desc. */
168 memset(desc, 0, UPIU_HDR_LEN);
169 desc->cmd_type_dd_irq = UTP_REQ_BUILD_CMD_DD_IRQ_FIELD(utrd_req->cmd_type, utrd_req->dd, utrd_req->irq);
170 desc->overall_cmd_status = utrd_req->ocs;
171 /* Bits 0 - 6 are reserved in cmd_desc_base_addr[0] field. */
172 desc->cmd_desc_base_addr[0] = ((uint32_t) utrd_req->req_upiu) & 0xC0;
173 desc->cmd_desc_base_addr[1] = ((uint32_t) utrd_req->req_upiu >> 8) & 0xFF;
174 desc->cmd_desc_base_addr[2] = ((uint32_t) utrd_req->req_upiu >> 16) & 0xFF;
175 desc->cmd_desc_base_addr[3] = ((uint32_t) utrd_req->req_upiu >> 24) & 0xFF;
176 desc->resp_upiu_offset = ROUNDUP(utrd_req->req_upiu_len, UPIU_HDR_LEN) / 4;
177 desc->resp_upiu_len = utrd_req->resp_upiu_len;
178
179 if (utrd_req->dd != UTRD_NO_DATA_TRANSFER)
180 {
181 /* Data transfer command.
182 * Fill in PRDT data.
183 */
184 desc->prdt_offset = utrd_req->prdt_offset / 4;
185 desc->prdt_len = utrd_req->prdt_len;
186 }
187
188 /* Flush UTRD to memory. */
189 cache_clean_invalidate_unaligned_start_addr((addr_t)desc, sizeof(struct utp_trans_req_desc));
190}
191
192static 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)
193{
194 struct utp_trans_req_desc *desc = NULL;
195 uint32_t door_bell_slot;
196
197 if (mutex_acquire(&(dev->utrd_data.bitmap_mutex)))
198 {
199 goto utp_get_desc_slot_addr_err;
200 }
201
202 *door_bell_val = utp_get_door_bell_bit(UFS_UTRLDBR(dev->base), &dev->utrd_data.bitmap, &door_bell_slot);
203 if (!(*door_bell_val))
204 {
205 goto utp_get_desc_slot_addr_err;
206 }
207
208 if (mutex_release(&(dev->utrd_data.bitmap_mutex)))
209 {
210 goto utp_get_desc_slot_addr_err;
211 }
212
213 desc = (struct utp_trans_req_desc *) ((addr_t)dev->utrd_data.list_base_addr + (door_bell_slot - 1) * sizeof(struct utp_trans_req_desc));
214
215utp_get_desc_slot_addr_err:
216 return desc;
217}
218
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -0700219int utp_poll_utrd_complete(struct ufs_dev *dev)
220{
221 int ret;
222 struct ufs_req_irq_type irq;
Sridhar Parasuramea079c02014-10-09 16:37:27 -0700223 uint32_t val, base, retry = 0;
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -0700224 base = dev->base;
225 val = readl(UFS_IS(base));
226 irq.irq_handled = 0;
227 /* Wait till the desc has been processed. */
228 while(((val & UFS_IS_UTRCS) == 0) && ((val & UFS_IS_UTMRCS) == 0))
229 {
230 val = readl(UFS_IS(base));
Sridhar Parasuramea079c02014-10-09 16:37:27 -0700231 retry++;
232 udelay(1);
233 if(retry == UTP_MAX_COMMAND_RETRY)
234 {
235 dprintf(CRITICAL, "UTP command never completed.\n");
236 return 1;
237 }
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -0700238#ifdef DEBUG_UFS
239 dprintf(INFO, "Waiting for UTRCS/URMRCS Completion...\n");
240#endif
241 }
242 if (readl(UFS_IS(base)) & UFS_IS_UTRCS)
243 {
244 val = readl(UFS_IS(base)) & UFS_IS_UTRCS;
245 writel(UFS_IS_UTRCS, UFS_IS(base));
246 irq.irq_handled = UFS_IS_UTRCS;
247 irq.list = &(dev->utrd_data.list_head.list_node);
248 irq.door_bell_reg = UFS_UTRLDBR(base);
249 utp_process_req_completion(&irq);
250 ret = INT_NO_RESCHEDULE;
251 }
252 else if (readl(UFS_IS(base)) & (UFS_IS_UTMRCS))
253 {
254 val = readl(UFS_IS(base)) & UFS_IS_UTMRCS;
255 writel(UFS_IS_UTMRCS, UFS_IS(base));
256 irq.irq_handled = UFS_IS_UTMRCS;
257 irq.list = &(dev->utmrd_data.list_head.list_node);
258 utp_process_req_completion(&irq);
259 ret = INT_NO_RESCHEDULE;
260 }
261 return ret;
262}
263
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700264static int utp_enqueue_utrd(struct ufs_dev *dev, struct utp_utrd_req_build_type *utrd_req)
265{
266 int ret;
267 struct utp_trans_req_desc *desc;
268 event_t utrd_evt;
269 struct ufs_req_node req;
270 uint32_t door_bell_bit_val;
271 struct utp_bitmap_access_type bitmap_req;
272
273 ret = UFS_SUCCESS;
274
275 event_init(&utrd_evt, false, EVENT_FLAG_AUTOUNSIGNAL);
276
277 desc = utp_get_desc_slot_addr(dev, utrd_req, &door_bell_bit_val);
278 if (!desc)
279 {
280 ret = UFS_FAILURE;
281 goto utp_enqueue_utrd_err;
282 }
283
284 /* Check register UTRLRSR and make sure it is read ‘1’ before continuing. */
285 if (!readl(UFS_UTRLRSR(dev->base)))
286 {
287 ret = -UFS_FAILURE;
288 goto utp_enqueue_utrd_err;
289 }
290
291 utp_enqueue_utrd_fill_desc(desc, utrd_req);
292
293 req.door_bell_bit = door_bell_bit_val;
294 req.event = &utrd_evt;
295
296 /* Enqueue the req in the device utrd list. */
297 list_add_head(&(dev->utrd_data.list_head.list_node), &(req.list_node));
298
299 dsb();
300
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -0700301#ifdef DEBUG_UFS
302 // print IS before write
303 ufs_dump_is_register(dev);
304#endif
305
306 utp_ring_door_bell(UFS_UTRLDBR(dev->base), door_bell_bit_val);
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700307
308 dsb();
309
Sridhar Parasuram7357e9b2014-10-01 12:19:11 -0700310#ifdef DEBUG_UFS
311 // print IS after write
312 ufs_dump_is_register(dev);
313#endif
314 ret = utp_poll_utrd_complete(dev);
315
Deepa Dinamaniab9c2b92013-09-12 11:30:58 -0700316 if (ret)
317 {
318 if (ret == ERR_TIMED_OUT)
319 {
320 /* Transaction not completed even after timeout ms. */
321 ret = utp_utrd_process_timeout_req(dev, utrd_req, &req);
322 }
323 else
324 ret = -UFS_FAILURE;
325 goto utp_enqueue_utrd_err;
326 }
327 else
328 {
329 /* Reset ret before returning. */
330 ret = UFS_SUCCESS;
331
332 /* Force read UTRD from memory. */
333 dsb();
334 cache_clean_invalidate_unaligned_start_addr((addr_t) desc, sizeof(struct ufs_req_node));
335
336 /* Check the response. */
337 if (desc->overall_cmd_status != UTRD_OCS_SUCCESS)
338 {
339 dprintf(CRITICAL, "Command failed. command type = %x\n", utrd_req->cmd_type);
340 ret = -UFS_FAILURE;
341 goto utp_enqueue_utrd_err;
342 }
343 }
344
345 /* Signal slot as free. */
346 bitmap_req.bitmap = &dev->utrd_data.bitmap;
347 bitmap_req.door_bell_bit = req.door_bell_bit;
348 bitmap_req.mutx = &(dev->utrd_data.bitmap_mutex);
349
350 ret = utp_remove_from_bitmap(&bitmap_req);
351 if (ret)
352 goto utp_enqueue_utrd_err;
353
354utp_enqueue_utrd_err:
355 return ret;
356}
357
358static int utp_get_prdt_len(uint32_t data_len, uint32_t *num_prdt)
359{
360 /* Calculate the prdt entries required. */
361 *num_prdt = ROUNDUP(data_len, UTP_MAX_PRD_DATA_BYTE_CNT);
362 *num_prdt >>= UTP_MAX_PRD_DATA_BYTE_CNT_BYTE_SHIFT;
363
364 if (*num_prdt > UTP_MAX_PRD_TABLE_ENTRIES)
365 {
366 dprintf(CRITICAL, "Data length exceeds for a single upiu transfer.\n");
367 return -UFS_FAILURE;
368 }
369
370 return UFS_SUCCESS;
371}
372
373static int utp_fill_req_upiu(struct ufs_dev *dev, struct upiu_req_build_type *upiu_data, struct upiu_gen_hdr *req_upiu)
374{
375 memset(req_upiu, 0, UPIU_HDR_LEN);
376
377 if (upiu_data->trans_type == UPIU_TYPE_QUERY_REQ)
378 {
379 /* Fill in query specific fields. */
380 if (utp_build_query_req_upiu((struct upiu_trans_mgmt_query_hdr *) req_upiu, upiu_data))
381 {
382 return -UFS_FAILURE;
383 }
384 }
385
386 /* If a data transfer cmd, check the alignment and length of the buffer. */
387 if (upiu_data->expected_data_len)
388 {
389 if (upiu_data->data_buffer_addr & 0x3)
390 {
391 return -UFS_FAILURE;
392 }
393 }
394
395 req_upiu->basic_hdr.trans_type = upiu_data->trans_type;
396 req_upiu->basic_hdr.flags = upiu_data->flags;
397 req_upiu->basic_hdr.cmd_set_type = upiu_data->cmd_set_type;
398 req_upiu->basic_hdr.data_seg_len = upiu_data->data_seg_len;
399 req_upiu->basic_hdr.lun = upiu_data->lun;
400 req_upiu->basic_hdr.total_ehs_len = upiu_data->ehs_len;
401 req_upiu->basic_hdr.task_tag = atomic_add((int *) &(dev->utrd_data.task_id), 1);
402 if (upiu_data->cdb)
403 memcpy(&(req_upiu->trans_specific_fields[4]), (void *) upiu_data->cdb, 16);
404 /* If command upiu, fill in data length. */
405 if (req_upiu->basic_hdr.trans_type == UPIU_TYPE_COMMAND)
406 ((struct upiu_cmd_hdr *)req_upiu)->data_expected_len = BE32(upiu_data->expected_data_len);
407
408 return UFS_SUCCESS;
409
410}
411
412static void utp_fill_utrd_properties(struct upiu_req_build_type *upiu_data,
413 struct utp_utrd_req_build_type *utrd,
414 struct utrd_cmd_desc *cmd_desc)
415{
416 utrd->cmd_type = upiu_data->cmd_type;
417 utrd->dd = upiu_data->dd;
418 utrd->irq = UTRD_IRQ_CMD;
419 utrd->prdt_offset = UPIU_HDR_LEN + cmd_desc->resp_upiu_len;
420 utrd->prdt_len = cmd_desc->num_prdt;
421 utrd->req_upiu = (struct upiu_basic_hdr *) cmd_desc->req_upiu;
422 utrd->req_upiu_len = UPIU_HDR_LEN;
423 utrd->resp_upiu_len = cmd_desc->resp_upiu_len;
424 utrd->ocs = 0xF;
425 utrd->timeout = upiu_data->timeout_msecs;
426}
427
428static void utp_fill_prdt_entries(struct upiu_req_build_type *upiu_data, struct utp_prdt_entry *prdt_entry)
429{
430 uint64_t buf;
431 uint64_t bytes_remaining;
432 uint32_t prd_dbc;
433
434 buf = upiu_data->data_buffer_addr;
435 bytes_remaining = upiu_data->expected_data_len;
436
437 while (bytes_remaining)
438 {
439 prdt_entry->data_base_addr = buf;
440 prdt_entry->data_upper_addr = buf >> 32;
441 prd_dbc = MIN(UTP_MAX_PRD_DATA_BYTE_CNT, bytes_remaining) - 1;
442 prdt_entry->data_byte_cnt = prd_dbc;
443 if (bytes_remaining <= UTP_MAX_PRD_DATA_BYTE_CNT)
444 break;
445 buf += UTP_MAX_PRD_DATA_BYTE_CNT;
446 bytes_remaining -= UTP_MAX_PRD_DATA_BYTE_CNT;
447 prdt_entry++;
448 }
449
450}
451
452int utp_enqueue_upiu(struct ufs_dev *dev, struct upiu_req_build_type *upiu_data)
453{
454 struct upiu_gen_hdr *req_upiu;
455 struct utp_utrd_req_build_type utrd;
456 uint32_t num_prdt;
457 struct utp_prdt_entry *prdt_entry;
458 int ret = UFS_SUCCESS;
459 uint32_t resp_len;
460 uint32_t cmd_desc_len;
461 struct utrd_cmd_desc cmd_desc;
462
463 /* Round up resp_upiu_len to a DWORD boundary.
464 * Also, make sure it is of min required length.
465 */
466 resp_len = ROUNDUP(upiu_data->resp_data_len, 4) + UPIU_HDR_LEN;
467
468 if (utp_get_prdt_len(upiu_data->expected_data_len, &num_prdt))
469 return -UFS_FAILURE;
470
471 /* Calculate the length. */
472 cmd_desc_len = UPIU_HDR_LEN + resp_len + num_prdt * sizeof(struct utp_prdt_entry);
473
474 /* Allocate memory for UTP Command Descriptor. */
475 req_upiu = (struct upiu_gen_hdr*) memalign((size_t ) lcm(CACHE_LINE, UTP_CMD_DESC_BASE_ALIGNMENT_SIZE), ROUNDUP(cmd_desc_len, CACHE_LINE));
476 if (!req_upiu)
477 {
478 dprintf(CRITICAL, "Unable to allocate request upiu\n");
479 return -UFS_FAILURE;
480 }
481
482 /* Fill req upiu. */
483 ret = utp_fill_req_upiu(dev, upiu_data, req_upiu);
484 if (ret)
485 {
486 goto utp_enqueue_upiu_err;
487 }
488
489 /* Fill UTRD properties. */
490 cmd_desc.num_prdt = num_prdt;
491 cmd_desc.req_upiu = req_upiu;
492 cmd_desc.resp_upiu_len = resp_len;
493 utp_fill_utrd_properties(upiu_data, &utrd, &cmd_desc);
494
495 prdt_entry = (struct utp_prdt_entry *) ((uint32_t) req_upiu + UPIU_HDR_LEN + resp_len);
496
497 /* Fill PRDT entries. */
498 if (num_prdt)
499 utp_fill_prdt_entries(upiu_data, prdt_entry);
500
501 /* Flush req_upiu */
502 dsb();
503 arch_clean_invalidate_cache_range((addr_t) req_upiu, cmd_desc_len);
504
505 /* Check the response. */
506 ret = utp_enqueue_utrd(dev, &utrd);
507 if (ret)
508 {
509 dprintf(CRITICAL, "Command failed. command = %x\n", req_upiu->basic_hdr.trans_type);
510 goto utp_enqueue_upiu_err;
511 }
512
513 /* UPIU processed. Invalidate cache to update resp. */
514 arch_invalidate_cache_range((addr_t) req_upiu, cmd_desc_len);
515
516 /* Save the response. */
517 memcpy(upiu_data->resp_ptr, (void *) ((uint32_t)req_upiu + UPIU_HDR_LEN), upiu_data->resp_len);
518 memcpy((void *) upiu_data->resp_data_ptr, (void *) ((uint32_t)req_upiu + 2 * UPIU_HDR_LEN), upiu_data->resp_data_len);
519
520utp_enqueue_upiu_err:
521 free(req_upiu);
522 return ret;
523}